Earlier quoted context omitted.
Perhaps the programmer wanted to run test functions on the objects as he deleted them? Or perhaps the data structure is a vector of pointers in C++, in which case using the standard clear method would introduce a massive memory leak. There are a lot of reasons why someone doing difficult work with complex objects would use a loop to delete them, and using invariants is only possible if you have immutable data structu…
> using invariants is only possible if you have immutable data structures. Wow, dude, maybe if you don't know what the words mean, you shouldn't argue about them.
What's wrong with this code, really?
181–190 of 217 posts
Re: What's wrong with this code, really?
#182Earlier quoted context omitted.
Perhaps the programmer wanted to run test functions on the objects as he deleted them? Or perhaps the data structure is a vector of pointers in C++, in which case using the standard clear method would introduce a massive memory leak. There are a lot of reasons why someone doing difficult work with complex objects would use a loop to delete them, and using invariants is only possible if you have immutable data structu…
> Perhaps the programmer wanted to run test functions on the objects as he deleted them? Doesn't justify anything that was done. > Or perhaps the data structure is a vector of pointers in C++, in which case using the standard clear method would introduce a massive memory leak. Doesn't justify anything that was done. > There are a lot of reasons why someone doing difficult work with complex objects would use a loop to…
readability < does it work
Re: What's wrong with this code, really?
#183Earlier quoted context omitted.
No, I pretty much just recoiled in horror immediately. 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!
What if someone changes "int" to "uint"? It wouldn't even infinite loop / crash... It would remove exactly one element! I can't see that. If i is a uint, at the end of the first cycle, i-- changes i to the maximum uint, then i++ changes i to 0, then i while ( 0
Re: What's wrong with this code, really?
#184/* Can't find a clear/remove method, don't have time to screw around with it now, might revisit later, might not. Sorry. */
Re: What's wrong with this code, really?
#185When pressed for a deadline, a demo, a functioning "something" - I do stuff that might not be the right way to do things because I need to get it to work. If the code ever has a chance of being witnessed by someone else, I always try to: /* Can't find a clear/remove method, don't have time to screw around with it now, might revisit later, might not. Sorry. */
Re: What's wrong with this code, really?
#186Re: What's wrong with this code, really?
#187The 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…
I am not a programmer, so I did not know about the clear() method. I thought it was a rather elegant solution! I guess it's all about perspective.
Re: What's wrong with this code, really?
#188Re: What's wrong with this code, really?
#189Earlier quoted context omitted.
The other nice thing about running it in reverse is that the .count method (assuming it's a method and not a property), needs only to be accessed in the initial condition setup. If you're accessing the size of, say, a linked list, you end up sneaking an O(n^2) runtime because it has to re-count the size of the list every iteration to check for termination. c.f. https://secure.wikimedia.org/wikipedia/en/wiki/Schlemiel…
Though you have to be careful with that, because the obvious code for removing from a collection in reverse: for(i = list.size; i >= 0; i--) list.removeAt(i); Is itself a schlemiel algorithm on any singly-linked list...
Re: What's wrong with this code, really?
#190Good grief. You are in a sorry environment. What are you doing there? I will fire anyone that writes code like that and checks it in, and then fire anyone who objects to me flagging it in a code review.