6 ms·
The code was unacceptable from line 1 onwards, and would not pass code review with me on watch. For one, for loops have invariants. Next up, you are altering
by KirinDave 15y ago
The code was unacceptable from line 1 onwards, and would not pass code review with me on watch.
For one, for loops have invariants.
Next up, you are altering the loop bound in the loop. I do not care if the code works, the code is hard to reason about when it goes all non-linear like that.
Finally, there is a goddamn Clear method.
That is the kind of code you see the next morning and delete; hoping none of your peers review the version control log that closely.
- trevelyan 15y agoPerhaps 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 structures.
- KirinDave 15y ago> 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 delete them Then use a while loop, and make this explicit. While loops have tests, for loops have invariants. There is a reason both exist, and it's not purely historical. > and using invariants is only possible if you have immutable data structures. Incorrect.
- trevelyan 15y agoIf 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
- KirinDave 15y ago"readability < does it work" No. In a professional setting, readability ~= does it work. The code presented works almost by accident. It is clear there is no thought put into it at all. It is confusing, and subsequent modifications could cause errors very easily. It is not acceptable code. Of course, code needs to work. Of course, if this .Clear method is a memory leak you wouldn't use it. Of course, we need to consider these things. But there is no situation in which this code is acceptable. This code would not even warrant a passing grade in an "Introduction To Programming" class.
- trevelyan 15y ago> Of course, if this .Clear method is a memory leak you wouldn't use it. Yes. Exactly.
- kragen 15y ago> 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 15y ago[deleted]
- quotemstr 15y agoWhat business do you have using a vector of bare pointers anyway? If you use smart pointers, the container's clear() will automatically free resources held by container elements.
- trevelyan 15y agoDesire to avoid garbage collection and automatic memory management mostly.