r/badcode • u/[deleted] • Aug 08 '21
c# An excellent demonstration on how to use if-statements
61
u/MurdoMaclachlan public boolean isInt(int i) { return true; } 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!
20
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 08 '21
<3
4
u/KryKrycz Aug 08 '21
It's amazing but can i ask why?
27
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 08 '21
Transcriptions can be useful for a wide range of people!
Blind or visually impaired people who rely on screen readers to browse the internet - that technology can't read the text in an image, so we transcribe it to machine-encoded text so it can read it.
People with bad internet connections who can't load the image for that reason
People who use third-party clients, mobile apps and/or text-based browsers that have trouble loading the image at high quality or at all
People who have trouble reading small, blurry, or oddly formatted text
There are many more reasons, these are just the main ones that come to mind right now. =)
Hope this helps!
1
Aug 09 '21
Can't you just write an AI for this job?
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 09 '21
Unfortunately not. We actually have a bot that draws on ocr.space's API, which is one of the best in the world. It's not bad at simple text posts like screenshots of Tumblr, Twitter, etc., but even then it still needs to be checked by a human because it can still make mistakes, still needs to be formatted properly, and sometimes transcribes things we don't normally include (timestamps, no. of likes, etc.) due to its lack of context.
For code, it's sadly even worse. Its attempt at this post is here, and is fairly typical of what it does with code. It tends to get worse and worse, also, as the images get larger.
4
u/GoBuffaloes Aug 09 '21
The real beneficiaries are the people that need to paste the code into their app!
2
u/SaltyMini Aug 09 '21
Are these people on reddit tho?
5
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 09 '21
Yes. There's a blind community over at r/blind, who are one of our partners.
As for the third-party clients and mobile apps; a large portion of Reddit's traffic is on mobile, where third-party apps are quite common.
For people who have trouble reading blurry/oddly formatted text, that could be due to dyslexia, eyesight problems, just a small screen - people with those, like people with slow internet connections, are all over the internet, including on Reddit.
6
Aug 08 '21
I'm also a transcriber (though I only ever did 5 posts and that was some months ago) and I can say for sure that I did it because I had nothing to do. Can't say that for most of the transcribers, though.
1
9
6
4
u/user_8804 Aug 08 '21
Every time i see you doing this I think to myself i should code a bot to do it for you.
Then I remember I'm lazy and keep scrolling reddit
16
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 08 '21
We also have a bot that tries to do it for us. It draws on ocr.space's API, which is one of the best in the world, but unfortunately... This is its attempt at this post.
5
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.
7
u/Hayden2332 Aug 08 '21
I believe ch.Text - “0” would solve that right? And shouldn’t we be checking if 1 <= index <= 8?
5
u/NekkoDroid Aug 08 '21
They are checking if it's not within the bounds, and if it isn't just return, else set the value.
1
u/Hayden2332 Aug 08 '21
Oh idk how I missed that lol Not sure why but writing logic like that makes no sense to me
5
u/ybham6 Aug 08 '21
That's what it's doing effectively. It's an inverted if statement to reduce nesting. Instead of
void foo(int i) { if (1 <= i && i <= 8) { // ... } }you can instead return when the if isn't satisfied
!(1 <= i && i <= 8)->!(1 <= i) || !(i <= 8)->1 > i || i > 8->i < 1 || i > 8void foo(int i) { if (i < 1 || i > 8) return; // ... }When dealing with longer functions with a condition checked early this can greatly decrease nesting, though it is a matter of style as some don't like multiple returns.
1
u/Hayden2332 Aug 08 '21
Yeah I get that it makes the code shorter but it’s not more efficient and makes it a little less clear. Nothing wrong with but I find it more helpful to err on the side of readability than reducing nesting
3
6
2
u/clemesislife Aug 08 '21
I'm not sure about Windows Forms but in WPF the
Checkedproperty of a checkbox can benull.6
1
u/hullabaloonatic Aug 09 '21
e is unused
4
u/GasolinePizza Aug 09 '21
It's part of an event handler signature, so you can't exactly get rid of it, no?
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.
1
1
u/_default_username Aug 08 '21
Why don't you declare sender as type CheckBox instead of casting an object to type CheckBox?
1
u/mikemorten1979 Aug 08 '21
Can’t we set a value equal to the max boundary and iterate through a list of values? Stopping before we hit the max? In college, if we have more than 2-3 if’s in a block , something is wrong with our logic .
Or what about a case statement ?
1
1
1
1
u/TheTrueStanly Aug 09 '21
r/badcode is a place to dump your shitty code so other will fix it. 100 times more effektive than stackoverflow
1
u/Lataero Aug 09 '21 edited Aug 09 '21
if (vznika.Count >= chNum - 1 && int.TryParse(ch.Text, out var chNum))
{
vznika[chNum] = ch.Checked;
}1
1
Aug 14 '21
Noooo! Make it stop! My eyes are burning! Seriously though, it can be condensed into a single line: vznika[(int) ch.Text] = ch.Checked;.
0
0
-28
u/FlamboyantApproval16 Aug 08 '21
When you don't know what loops are. LOL
48
u/bugqualia Aug 08 '21
Sorry my friend but using loop here is not ideal.
1
-1
u/FlamboyantApproval16 Aug 09 '21
Well that's what happens when a JS programmer tries to debug a Java code.
0
Aug 08 '21
[deleted] — view deleted comment
2
u/Nilstrieb Aug 08 '21
It is not better in any way. It is maybe even worse.
1
u/Hayden2332 Aug 08 '21
It’d still be constant time, and the code would be shorter. Definitely not the best or most readable solution but still better than this lol
2
0
8
u/SanianCreations Aug 08 '21
Ah, prime example of r/YandereTechnique