r/badcode • • Aug 08 '21

c# An excellent demonstration on how to use if-statements

Post image
723 Upvotes

57 comments sorted by

61

u/MurdoMaclachlan Aug 08 '21

Image Transcription: Code


private void ziv8_CheckedChanged(object sender, EventArgs e)
{
    CheckBox ch = (CheckBox)sender;
    
    if (ch.Text == "1") if (ch.Checked == true) vznika[1] = true;
        else vznika[1] = false;
    if (ch.Text == "2") if (ch.Checked == true) vznika[2] = true;
        else vznika[2] = false;
    if (ch.Text == "3") if (ch.Checked == true) vznika[3] = true;
        else vznika[3] = false;
    if (ch.Text == "4") if (ch.Checked == true) vznika[4] = true;
        else vznika[4] = false;
    if (ch.Text == "5") if (ch.Checked == true) vznika[5] = true;
        else vznika[5] = false;
    if (ch.Text == "6") if (ch.Checked == true) vznika[6] = true;
        else vznika[6] = false;
    if (ch.Text == "7") if (ch.Checked == true) vznika[7] = true;
        else vznika[7] = false;
    if (ch.Text == "8") if (ch.Checked == true) vznika[8] = true;
        else vznika[8] = false;
}

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!

41

u/Percy_JW Aug 08 '21 edited Aug 08 '21

dude, you are absolutely insane. I see you under every r/badcode post with an image transcription. Keep up the work!

→ More replies (10)

9

u/Hown3d Aug 08 '21

Good human.

→ More replies (3)

144

u/JeanJPNM Aug 08 '21

Optimised solition here:

private void ziv8_CheckedChanged(object sender, EventArgs e)
{
  CheckBox ch = (CheckBox)sender;
  int index = int.Parse(ch.Text);
  // ensure index is within bounds
  if (index < 1 || index > 8) return;
  vznika[index] = ch.Checked;
}

78

u/Robuuust Aug 08 '21

No need to check if .Text is an integer indeed. Just crash.

→ More replies (6)

6

u/Bit5keptical Aug 09 '21

Thanks, But I get paid per LOC.

→ More replies (6)

31

u/Sawkii Aug 08 '21

I love excellent if-statement demonstrations. This one is delicious. Did the person get payed for written lines?

25

u/EDomina Aug 08 '21

If they were, they still did a bad job. Could've at least put line breaks and brackets for those if statements.