Live data from Hacker News

What's wrong with this code, really?

cvmountain.com

181–190 of 217 posts

Re: What's wrong with this code, really?

#181
post #167

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.

[deleted]

Re: What's wrong with this code, really?

#182

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…

> 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…

If someone used clear on a vector of pointers AS RECOMMENDED IN THIS ARTICLE they've just introduced a serious, serious problem.

readability < does it work

Re: What's wrong with this code, really?

#183
post #13

Earlier 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

Oh, yep. That's correct, thanks.

Re: What's wrong with this code, really?

#184
When 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?

#185

When 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. */

I should add that I often make these comments even if my code doesn't have a chance of anyone else looking at it. I forget way too often why I did something and need my own reminders. For all we know, this guy may have actually had a good reason to do this (work-around, faster, ...) - a simple qualifying comment for doing something out of the ordinary would have explained it all.

Re: What's wrong with this code, really?

#187

The 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.

[deleted]

Re: What's wrong with this code, really?

#189
post #23
post #9

Earlier 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...

That really depends on the implementation of the linked list. I don't think it would actually behave O(n^2) in most implementations.

Re: What's wrong with this code, really?

#190
When I was asked to justify flagging this code

Good 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.

Post reply on HN