r/badcode • u/T_V_G_G • Jul 16 '21
js Found this in the source code for a reasonably large open source online experiment platform.
32
Jul 16 '21
The if statements are horrible and also would add additional condition checks, but let’s get passed that for a second, why would you write a for loop like that? He knew he needed the semi colons, he knew he needed to increment the index, what a tw0t.
Didn’t need conditions, all the code blocks do the same thing, also is he connectBot or adding a bot to a channel
3
u/T_V_G_G Jul 17 '21
I do not know how it works exactly. The bots run a script which can play the game as though they where a client. This could mean they run exactly like a client, thus connecting a bot and adding it to the channel or it could mean that an instance of the class is created and is added to the channel.
Either way all of them do the same thing.
2
u/_irobot_ Jul 17 '21
They do the same thing, but all the if statements are true if the needed bots is 4 or more, so that same thing happens several times. The if statement would don't add extra conditions because every iteration of a for loop has a condition check. If this was all in a for loop, and the compiler doesn't optimize it, then there would be a few more machine level instructions for each needed bot.
1
Jul 17 '21
He is checking if neededbots multiple times every time, and there is a for loop anyway with its own condition.
7
u/TheDoddler Jul 17 '21
The (somewhat poorly) unrolled loop is one thing, but what in the world is with that for loop syntax? I can see why someone might consider lifting the subtract out of the loop condition since it would calculate each time (for all the good saving a single subtract will do you), but everything else, modifying i in the condition, skipping the declaration and increment blocks for no reason, starting the loop at -1 because your condition increments before the first check, I don't get it.
6
u/T_V_G_G Jul 17 '21
This is part of the insanity. In my mind it takes more effort to write it like this than to just code it properly the first time. I just find myself asking all sorts of weird questions wondering how this individual thinks. Do they write all their loops like this? What happens when they "optimize" by unrolling more and forget to update the -3 in the neededBots reassignment? My best guess would be that they come from another programming language but I do not know of one where things should be written like this.
56
u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} Jul 16 '21 edited Jul 16 '21
Image Transcription: Code
if (neededBots >= 1) {
this.channel.connectBot(botOptions);
}
if (neededBots >= 2) {
this.channel.connectBot(botOptions);
}
if (neededBots >= 3) {
this.channel.connectBot(botOptions);
}
if (neededBots >= 4) {
i = -1;
neededBots = neededBots - 3;
for (; ++i < neededBots;) {
this.channel.connectBot(botOptions);
}
}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!
26
u/mostly_inefficient Jul 16 '21
Good human
16
u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} Jul 16 '21 edited Jul 01 '23
This comment has been removed in protest of Reddit's API changes. You can view a copy of it here.
8
u/circuit10 Jul 16 '21
You put 1 instead of i
14
u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} Jul 16 '21 edited Jul 01 '23
This comment has been removed in protest of Reddit's API changes. You can view a copy of it here.
29
9
Jul 16 '21
That's how you spot the dumb smart kid during code review.
1
1
u/WhatBoizz Jul 18 '21
Can someone help stupid me and tell me what’s so bad about this code?
2
u/T_V_G_G Jul 19 '21
The for loop is also poorly written. Should be the following and not the garbage that was written.
for (i=0;i<neededBots;i++)2
1
u/Itay_123_The_King Jul 19 '21
What's ++i?
1
u/T_V_G_G Jul 19 '21
Any time you use ++ it signifies to increment. If you use ++i then it will increment "i" before evaluating and if you use i++ it will "i" after evaluating. -- Works much the same way.
1
u/Itay_123_The_King Jul 19 '21
I'm used to working with i++ but I don't think I get ++i. Is it like doing
*Some condition involving i* i--; ```?1
u/T_V_G_G Jul 20 '21
Here is an example of the outputs:
i++ example:
var i=0;
var y=i++;
//y=0 but i = 1++i example:
var i=0;
var y=++i;
//y=1 and i = 1In the case of this for loop it means that i gets incremented before condition evaluation. The for loop would exit before running for the last time so the individual set i to -1 instead of 0, which is less readable.
If they wrote the for loop properly with ++i it would look as follows.
for (i=-1;i<neededBots;++i) //When written correctly unlike what they didIf they used i++ like a normal person
for (i=0;i<neededBots;i++) //Standard proper syntax1
u/Itay_123_The_King Jul 20 '21
Oh I see. May I ask what language is this? I recognize everything to be the same as C++ besides you using var instead of int or float or whatever.
1
u/T_V_G_G Jul 20 '21
The language is javascript and so is my example. However, this logic works in many programming languages that support the ++ operators, including c++.
Javascript auto handles variable types and then you have scope when declaring: const(immutable), var (global), let(block). Typically when coding stuff let is sufficient and sometimes actually better to avoid namespace issues.
1
3
2
1
1
64
Jul 16 '21
[removed] — view removed comment
21
u/T_V_G_G Jul 17 '21
Imagine my horror when I saw the first few lines and said "No! Why not just use a for loop?" only to see the for loop wrapped in the last clause... An oddly written for loop at that.
9
291
u/Szelwin Jul 16 '21
Daaamn... And here I am, thinking I'm not good enough to contribute in open source... Maybe now I will!