Flow Control 101
One of the first things you learn when you’re starting to develop software is flow control. By flow control, I mean if-statements, loops and that sort of thing. Imagine my surprise and subsequent urge to empty the contents of my stomach all over my keyboard when I ran across this today:
private void SetQuantityOnHand(Item forItem)
{
while (forItem.IsAutoReplenished)
forItem.QuantityOnHand = 0;
// Remainder of method removed for brevity.
}
When posting source code to this site I usually make an attempt to justify what the developer was thinking when he wrote the code in question, but this one is pretty hard to justify. Listen folks, while loops are awesome… when you have something to loop through! Although I feel that the solution to this one is pretty self-explanatory (or at least I hope that it is), here is how I wound up refactoring the code:
private void SetQuantityOnHand(Item forItem)
{
if (forItem.IsAutoReplenished)
forItem.QuantityOnHand = 0;
// Remainder of method removed for brevity.
}
That was painless enough. I usually conclude my posts with a helpful tip but, if you don’t know the difference between an if-statement and a loop, it might be time to hit the books again.
