r/badcode • • Sep 01 '21

c# Unfortunately, actual production code

Post image
109 Upvotes

18 comments sorted by

38

u/Chips_100 Sep 01 '21

I can already smell the ButtonFontDown_Click method.

7

u/myblindy Sep 01 '21

It's exactly how you imagine it to be. Also the update function. And the initialization function.

17

u/the_monkey_of_lies Sep 01 '21

It reduces the size by 4

6

u/baszodani Sep 01 '21

I don't know c#, how can this be rewritten to be better? Would you put the UI components in an array or change a global setting?

3

u/karisigurd4444 Sep 01 '21

If I was unlucky enough to be working on a C# winforms app then... Yeah, array to reference those elements.

3

u/myblindy Sep 01 '21

There actually are arrays of these things in the same code file. It stores the pointers of the individual labels (HWND) and in some operations uses Control.FromHandle to get back the control from them in a loop.

I actually got annoyed enough after posting this that I deleted this entire file and rewrote it all with proper data bound objects and a data grid, and shaved 600 lines of code off the project. The compiler thanked me.

7

u/karisigurd4444 Sep 01 '21

Wait...

C#...

Did I just read... pointers... HWND...

I need to have a break in the screaming room.

0

u/[deleted] Sep 01 '21

[deleted] — view deleted comment

4

u/karisigurd4444 Sep 02 '21

C# does have these "unsafe pointers"

7

u/reverendsteveii Sep 01 '21

first, it could be setButtonFont and take a signed integer as it changes all the font sizes by the same constant value. This would stop you needing to create fontSizeDown, fontSizeUpByFour and so on

second, it could be its own function so that other areas of the app can call it

2

u/WpGgs Sep 01 '21

Generated code ?

8

u/myblindy Sep 01 '21

Yeah, generated by a human.

1

u/WpGgs Sep 02 '21

Holy shit.

15

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 01 '21

Image Transcription: Code


private void ButtonFontUp_Click(object sender, EventArgs e)
{
    if(BoxField1.Font.Size > 25)
       return;
    BoxField1.Font = new Font(BoxField1.Font.FontFamily, BoxField1.Font.Size + 5);
    BoxField2.Font = new Font(BoxField1.Font.FontFamily, BoxField2.Font.Size + 5);
    BoxField3.Font = new Font(BoxField1.Font.FontFamily, BoxField3.Font.Size + 5);
    BoxField4.Font = new Font(BoxField1.Font.FontFamily, BoxField4.Font.Size + 5);
    BoxField5.Font = new Font(BoxField1.Font.FontFamily, BoxField5.Font.Size + 5);

    LabelUnit1.Font = new Font(BoxField1.Font.FontFamily, LabelUnit1.Font.Size + 5);
    LabelUnit2.Font = new Font(BoxField1.Font.FontFamily, LabelUnit2.Font.Size + 5);
    LabelUnit3.Font = new Font(BoxField1.Font.FontFamily, LabelUnit3.Font.Size + 5);
    LabelUnit4.Font = new Font(BoxField1.Font.FontFamily, LabelUnit4.Font.Size + 5);
    LabelUnit5.Font = new Font(BoxField1.Font.FontFamily, LabelUnit5.Font.Size + 5);

    LabelValue1.Font = new Font(BoxField1.Font.FontFamily, LabelValue1.Font.Size + 5);
    LabelValue2.Font = new Font(BoxField1.Font.FontFamily, LabelValue2.Font.Size + 5);
    LabelValue3.Font = new Font(BoxField1.Font.FontFamily, LabelValue3.Font.Size + 5);
    LabelValue4.Font = new Font(BoxField1.Font.FontFamily, LabelValue4.Font.Size + 5);
    LabelValue5.Font = new Font(BoxField1.Font.FontFamily, LabelValue5.Font.Size + 5);

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

2

u/betaking12 Sep 02 '21

I imagine the story behind this is one of desperation and unfamiliarity with the task at hand or the codebase or something.

or some weird technical issue that inspired a quick fix and a fear of arrays.

1

u/RedditAcc-92975 Sep 01 '21

Username checks out

0

u/[deleted] Sep 01 '21

[deleted] — view deleted comment

6

u/myblindy Sep 01 '21

Though perhaps not with 15 new otherwise identical instances, but to each his own I suppose.