Article 4Q64E CodeSOD: Time to Wait

CodeSOD: Time to Wait

by
Remy Porter
from The Daily WTF on (#4Q64E)

When dealing with customers- and here, we mean, "off the street" customers- they often want to know "how long am I going to have to wait?" Whether we're talking about a restaurant, a mechanic, a doctor's office, or a computer/phone repair shop, knowing (and sharing with our customers) reasonable expectations about how much time they're about to spend waiting.

Russell F works on an application which facilitates this sort of customer-facing management. It does much more, too, obviously, but one of its lesser features is to estimate how long a customer is about to spend waiting.

This is how that's calculated:

TimeSpan tsDifference = dtWorkTime - DateTime.Now;string strEstWaitHM = ((tsDifference.Hours * 60) + tsDifference.Minutes).ToString();if (Convert.ToInt32(strEstWaitHM) >= 60){decimal decWrkH = Math.Floor(Convert.ToDecimal(strEstWaitHM) / 60);int intH = Convert.ToInt32(decWrkH);txtEstWaitHours.Value = Convert.ToString(intH);int intM = Convert.ToInt32(strEstWaitHM) - (60 * intH);txtEstWaitMinutes.Value = Convert.ToString(intM);}else{txtEstWaitHours.Value = "";txtEstWaitMinutes.Value = strEstWaitHM;}

Hungarian Notation is always a great sign of bad code. It really is, and I think that's because it's easy to do, easy to enforce as a standard, and provides the most benefit when you have messy variable scoping and keeping track of what type a given variable is might actually be a challenge.

Or, as we see in this case, it's useful when you're passing the same data through a method with different types. We calculate the difference between the WorkTime and Now. That's the last thing in this code which makes sense.

The key goal here is that, if we're going to be waiting for more than an hour, we want to display both the hours and minutes, but if it's just minutes, we want to display just that.

We have that TimeSpan object, which as you can see, has a convenient Hours and Minutes property. Instead of using that, though, we convert the hours to minutes, add it together, if the number is more than 60, we know we'll be waiting for over an hour, so we want to populate the hours box, and the minutes box, so we have to convert back to hours and minutes.

In that context, the fact that we have to convert from strings to numbers and back almost seems logical. Almost. I especially like that they Convert.ToDecimal (to avoid rounding errors) and Math.floor the result (to round off). If only there were some numeric type that never rounded off, and always had an integer value. If only"

proget-icon.png [Advertisement] ProGet supports your applications, Docker containers, and third-party packages, allowing you to enforce quality standards across all components. Download and see how! TheDailyWtf?d=yIl2AUoC8zAIRZ104jR-dY
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