Saturday, October 27, 2007

When simple is just not enough...

We all have felt the pain at one time or another. Yes... you know it... its that warm fuzzy time when you have to maintain someone else's code.

Generally, its difficult enough to adapt your thought process/logic to that of someone else, however, the added fun comes when the person who actually created the masterpiece of code, did it with a complete disregard to any sort of afore mentioned logic.

Now please, don't get me wrong... I don't usually go around ragging on code that someone else has done to the best of his/her ability (in this particular case lets hope, for the sake of the creator, it was the result of something closer to an alcohol induced coma as opposed to a fair reflection of said ability), but sometimes, especially at 01:20 in the morning, having to sift through what can only be described as a waste of time, I am a little less in the forgiving mood...

I believe the following sums it up:

   1: CultureInfo cuInfo = CultureInfo.CurrentCulture;
   2: NumberFormatInfo numberFormatInfo = cuInfo.NumberFormat;
   3: string strNegativeSymbol = numberFormatInfo.NegativeSign;
   4: if ((dtbContract != null) && (dtbContract.Rows.Count > 0))
   5: {
   6:     decContracRate = Convert.ToDecimal(dtbContract.Rows[0]["PayoffAmount"],
   7:                        System.Globalization.CultureInfo.InvariantCulture);
   8: }
   9:  
  10: if ((decContracRate.ToString().IndexOf(strNegativeSymbol) > -1))
  11: {
  12:     btnPrintPayment.Enabled = false;
  13:     if (btnIsVoid.Enabled == true)
  14:     {
  15:         btnIsVoid.Enabled = false;
  16:     }
  17: }
  18: else
  19: {
  20:     btnPrintPayment.Enabled = true;
  21: }

Excluding all the other issues with this code snippet, unless I am missing something extremely sneaky - I am pretty sure the following would have done the trick:



   1: if( decContractRate < 0 )
   2: {
   3:     // oh look - its a negative
   4:     ...
   5: }

Yup... sometimes simple is just not enough...

No comments: