CodeSOD: Count on Me
by Remy Porter from The Daily WTF on (#5BM9D)
There is no task so simple that a developer can't find a harder, more wasteful, and pointless way to do it. For example, let's say you want to know how many rows are in a data grid object?
Hans found this C# snippet in production a few years ago:
int count = 0;for(int i = 0; i <= dgView.Rows.Count; i++){ count++;}
To find out how many rows are in our dgView, we start a for loop which counts from 0 to the... Rows.Count in the view. Worse, this has an off-by-one bug, as they use <= to compare, ensuring an extra iteration.
The old saying is "work smarter, not harder," but this code doesn't just work harder, it also works dumber.
[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!