CodeSOD: Twice, Thrice
Most (but clearly, not all) programmers have generally mastered the basic for loop. If you really want to separate the wheat from the chaff, ask them to write a for loop that counts by two, or three, or some other arbitrary number.
That's what happened at J Banana's workplace, where they needed to parse in tokens from a config file. They come in key value pairs, so the code needs to count by twos. This was their solution:
for(int i=0; i<values.size(); i= i++){ map.put(values.get(i), values.get(++i)); i++;}
At a quick glance, you may end up scratching your head: the ++ operator appears three times, but they want to count by twos, and yet this code works. It works, in part, because of the developer's incompetence.
In the body of the loop, values.get(i) gets the current item in the list, and values.get(++i) does a pre-increment on i, getting the next item in the list, just like it's supposed to. Finally, the i++ increments one more time, getting us the two increments that we need.
It's the for loop where our developer blundered into a working solution. i= i++ is a post increment. It sets i equal to the value stored in i, and then increments the value which used to be stored in i. It's the same as doing i=i. It's unintentionally tautological.
My suspicion is that the developer wrote code that didn't work, and then started stochastically adding symbols and expressions until it got the result they wanted. It's like ChatGPT, but less self-aware.
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!