RSS 2.0
# Saturday, February 13, 2010

Well, technically it is, but that’s not really my point in this.

Working on a project now that has everything coming in as a DataTable to the UI – single entity records, child entity records, lookup data – everything.  Using one for a simple ListBox or DropDownList is tolerable, but I still prefer to have my own class that I bind to.

I felt bad for the guy that had to deal with it, writing lines like these to deal with it:

NumberOfUnits = Convert.ToInt32(dt.Rows[0][FIELD_NUMBER_OF_UNITS].ToString());

txtLocation.Text = dt.Rows[0][FIELD_LOCATION].ToString();

Not only is this cumbersome and error prone (nevermind the unneeded ToString() in the first example), it is also not DRY at all.  Every time one of these things is needed, the same code was written to extract from the DataTable.  Of course, the argument was to “keep it simple” and we know that usually ends up not being simple.  Taking the extra small step of creating some entity classes that have a constructor that takes in a DataReader would have not only made the other developers life much easier, it would also allow for further extendibility, encapsulation, etc.

Saturday, February 13, 2010 9:33:29 PM UTC  #    Comments [0] -
.NET | ttsbd

I go through quite a bit of other people’s code and I see many things that could be done better and some things that simply shouldn’t be done.  I’m going to post about some of the things that shouldn’t be done (ttsbd).

A Date is a Date

A date is a date, so treat it as a date.  Don’t pass it around as a string.

String Emptiness

I see this a lot:

if (mystring == “”)

Well, if mystring actually equals “ “, that’s probably not what you want either.

Consider the follow:

  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string s = null;
  10.             Console.WriteLine("string s = null;");
  11.             TestS(s);
  12.             Console.WriteLine("");
  13.  
  14.             s = " ";
  15.             Console.WriteLine("s = \" \"");
  16.             TestS(s);
  17.  
  18.         }
  19.  
  20.         static void TestS(string s)
  21.         {
  22.             Console.WriteLine("\ts == \"\": " + (s == "").ToString());
  23.             Console.WriteLine("\ts == String.Empty: " + (s == String.Empty).ToString());
  24.             Console.WriteLine("\tString.IsNullOrEmpty(s): " + String.IsNullOrEmpty(s));
  25.             if (s != null)
  26.                 Console.WriteLine("\tString.IsNullOrEmpty(s.Trim()): " + String.IsNullOrEmpty(s.Trim()));
  27.         }
  28.     }
  29. }

I’ll post more ttsbds at different times.  These were just a couple that were bugging me recently.

Saturday, February 13, 2010 9:25:59 AM UTC  #    Comments [0] -
.NET | ttsbd
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
George Handlin
Sign In
Statistics
Total Posts: 51
This Year: 0
This Month: 0
This Week: 0
Comments: 9
All Content © 2012, George Handlin
DasBlog theme 'Business' created by Christoph De Baene (delarou)