3 ms·
This is perfectly valid code. You cannot modify a collection which is being iterated safely so it's the best way to handle the situation.
by pointyhat 15y ago
This is perfectly valid code. You cannot modify a collection which is being iterated safely so it's the best way to handle the situation.
- deleted 15y ago[deleted]
- mekoka 15y agoThe code works, the problem is that it's not very obvious why it's going about it the way it does. You look at it and right away ask yourself "wtf, did I miss something?" simply because it's so unusual. You might look at it for 2 minutes and figure it out, but that's 2 minutes too long for what's actually being accomplished. The problem is, if you were to come back and look at it again 6 months from now, it would take another 2 minutes. The point of the article isn't really the method that was used to get to the result, but rather the fact that code should be made easy to read and understand, because +60% of the time is spent maintaining it. I usually tell this to newbie programmers, "code is meant for people to read, machines understand on/off". Even if you need to borrow such a convoluted approach to clear a collection (as opposed to the more direct clear() method), there are simpler and more readable alternatives: while(Pages.count > 0){ Pages.Remove(Pages[0]); }
- pointyhat 15y agoThat's what comments are for. If something is ambiguous, then you should comment it. As for your approach, I do like that better.