r/badcode • • Jul 16 '21

js Found this in the source code for a reasonably large open source online experiment platform.

Post image
812 Upvotes

53 comments sorted by

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!

169

u/T_V_G_G Jul 16 '21

I checked and it used to be a for loop. The individual who did it marked it as "optimizations". I think it might be some ploy to seem like they contributed to many projects.

93

u/thesola10 Jul 16 '21

Back in the 90s this would have been the case. Nowadays modern compilers pick up these patterns automatically in order to avoid ugly code like on the post.

63

u/T_V_G_G Jul 16 '21

I agree. I guess it's the ugliness of it and the fact that micro optimizations like these are not useful. Unless your code is running poorly due to optimization issues, it's probably best to not spend time rewriting this kind of thing. Even if your code is running poorly it's very likely that your for loops are not the cause.

26

u/bernieBrogrammer Jul 16 '21

Premature optimization is the root of all evil - Donald Knuth

Fun c2 wiki page on this topic.

1

u/laundmo Jul 27 '21

everyone should be familiar with profiling, because it allows you to optimise what the actual issue is, when needed

10

u/danbulant Jul 17 '21

Also, microoptimizating JS is usually not worth it, as it's still considerably slower than for example C++.

8

u/[deleted] Jul 16 '21

What benefit to optimizing this code could there possibly be? Does it even run more than once per session?

7

u/T_V_G_G Jul 16 '21

Technically it could. Each time a game is started with bots it would. However the bot logic code would be way more intensive than that for loop. As bench marked by someone else this "optimization" doesn't really do much.

3

u/IrritableGourmet Jul 17 '21

I could possibly see it in something like a small microcontroller where the expense of setting up another variable and a loop in memory is more than just adding some conditionals. I've used the ATTiny85 a lot, which has 512B of RAM. Not giga-, mega-, or kilo-, just 512 bytes. You need to be very careful about how you're using that.

This is not that scenario.

9

u/SadWebDev Jul 16 '21

I would love to see how that statement holds up to a benchmark

13

u/circuit10 Jul 16 '21

8

u/circuit10 Jul 16 '21

It seems to change from run to run so it makes no real difference

18

u/T_V_G_G Jul 16 '21

Your example does indeed change from run to run. I would imagine that real world average neededBots would have an effect on which is better too. However in all cases the optimization is negligible and readability should be favored.

Thanks for running the test :)

22

u/iotasieve Jul 16 '21 edited Jul 16 '21

ah yes because for loop is clearly more expensive than a function that clearly connects to external net resource

3

u/GreeedyGrooot Jul 16 '21

That makes some sense. My first thought was: "They got the best solution for this. Why do it like this?"

1

u/[deleted] Jul 17 '21

I’m sure this optimization also came with tests. I’m not holding my breath.

1

u/[deleted] Jul 17 '21

Isn’t this an example of loop unraveling?

5

u/[deleted] Jul 16 '21

[removed] — view removed comment

6

u/T_V_G_G Jul 17 '21

From my understanding they had permission to modify the code and directly committed it to the master branch so no merge was involved.

32

u/[deleted] 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

u/[deleted] 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

u/circuit10 Jul 16 '21

You're not an idiot, everyone makes mistakes :)

9

u/[deleted] Jul 16 '21

That's how you spot the dumb smart kid during code review.

1

u/[deleted] Jul 17 '21

i think it’s just the dumb kid?

1

u/[deleted] Jul 21 '21

Yeah, but the dumb kid that read a blog post about loop unrolling.

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

u/[deleted] Jul 18 '21

The code in the 4th if statement should be the whole code

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 = 1

In 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 did

If they used i++ like a normal person

for (i=0;i<neededBots;i++) //Standard proper syntax

1

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

u/Itay_123_The_King Jul 21 '21

Oh ok thanks for the info

3

u/Ytrog Jul 17 '21

This is a twitch case statement 🤪

2

u/SorryDidntReddit Jul 17 '21

Is that a 4+ loop?

1

u/brdzgt Jul 18 '21

They were so close to enlightenment

1

u/pocketjokers87 Jul 18 '21

Hmm...checks out

64

u/[deleted] 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

u/daterkerjabs Jul 16 '21

Would a compiler catch this and just make it all a loop?