The code in question: for ( int i=0 ; i Nice analysis into the thinking that went into creating such bad code. Took me a while to even see the i-- at the bottom.
What's wrong with this code, really?
11–20 of 217 posts
Re: What's wrong with this code, really?
#12A better question would be, there's tremendous deadline pressure, the company is in imminent danger of losing a giant deal if we don't have working code for some demo, and you have three features to implement by Monday afternoon. Do you write a new feature immediately, open a ticket for refactoring this loop and then write a new feature, or rehearse your explanation to the big boss that over the lifetime of the software, rewriting the code before adding a new feature was more important?
http://raganwald.posterous.com/javas-comb-over
Just kidding, but trying to make the point that "what do you think of this code" is a little obvious as an interview question.
Re: What's wrong with this code, really?
#13Anyone else read it and on the first pass think, "yeah that works". Then on second pass think, "it's not a good idea, but it works." Then finally think, "under pressure I've done worse; at least this works as intended. S/he should probably comment it." Or is it only me? Addendum: I would also add that even as a junior program, Clear() was easily learned within the first few minutes and usually when you have to use a…
Even if I had to avoid .Clear(), there are immediately-obvious, better ways.
while ( !thing.Empty() )
thing.Remove( 0 );
In fact, the code isn't just bad; it's risky. What if someone changes "int" to "uint"? It wouldn't even infinite loop / crash... It would remove exactly one element!Re: What's wrong with this code, really?
#14The article's point about writing code that does what it says it does is fine. But as an interview question, I have trouble imagining that candidates won't figure out that this is a game of "guess the answer I'm looking for" and say that they would rewrite this code. A better question would be, there's tremendous deadline pressure, the company is in imminent danger of losing a giant deal if we don't have working code…
Re: What's wrong with this code, really?
#15The article's point about writing code that does what it says it does is fine. But as an interview question, I have trouble imagining that candidates won't figure out that this is a game of "guess the answer I'm looking for" and say that they would rewrite this code. A better question would be, there's tremendous deadline pressure, the company is in imminent danger of losing a giant deal if we don't have working code…
Most do say exactly that, then we have a chat about what they might do instead. Many immediately say, "um, is there a Clear or RemoveAll method?!" It's all over within a few minutes, we move on. The bigger picture is always going to trump the details, until the day the details have piled up and can no longer be ignored. It's the great big technical-debt elephant in the room.
Re: What's wrong with this code, really?
#16Anyone else read it and on the first pass think, "yeah that works". Then on second pass think, "it's not a good idea, but it works." Then finally think, "under pressure I've done worse; at least this works as intended. S/he should probably comment it." Or is it only me? Addendum: I would also add that even as a junior program, Clear() was easily learned within the first few minutes and usually when you have to use a…
Modifying a loop variable inside a for() loop is generally a very bad idea, that's a red flag for me.
Re: What's wrong with this code, really?
#17In C# / .Net you can't remove an element from an enumerator while you're enumerating through it. You can remove the last element however, as it's the final loop the enumerator isn't used again so it won't throw an error. The original developer probably tried to remove it forward only first, encountered an error and wrote the code to loop through it backwards, using the random tweaking technique.
What's rather depressing is that a lot of developers I've encountered use the random tweaking methodology, instead of figuring out what's really happening.
Re: What's wrong with this code, really?
#18 for ( int i=0 ; i Re: What's wrong with this code, really?
#19I would hope that in those kinds of situations I would remember to add a FIXME comment so that I would come back in saner times and make it nice.
Re: What's wrong with this code, really?
#20Deleting from a container while you are iterating over it should always raise red flags.