Article 5BM9D CodeSOD: Count on Me

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.

otter-icon.png [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today! TheDailyWtf?d=yIl2AUoC8zAWQkizwUBlNU
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments