3 ms·
> 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 structu
by 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.