Tag Archives: refactoring

Refactoring the inefficient data loop

OK. Last one for today.  I’m not going to go into too much detail except to say that the offending piece of code loads all items in a ‘matrix’ database table and builds a business object collection hierarchy.  This table has 4 fields, one or more of which may be NULL – for different meanings.  [...]

Posted in C#, Refactoring, SQL Server | Also tagged , | Comments closed

Refactoring the blindingly obvious – Enums and Value Names

I know many people have written about this one, but it’s cropped up yet again in the app I’m maintaining.  The old chestnut of setting a string based on an enum value – when the enum names are identical to the string values (most often with one word names).  public enum ItemStatus{    Draft = 1,    Pending = 2,    Released = 3,    Recalled = 4,    Rejected = 5,}switch (item.ItemStatus){    case ItemStatus.Draft:        {            statusLabel.Text = “Draft”;            break;        }    case ItemStatus.Pending:        {            statusLabel.Text = “Pending”;            break;        }    case ItemStatus.Released:        {            statusLabel.Text = “Released”;            break;        }    case ItemStatus.Recalled:        {            statusLabel.Text = “Recalled”;            break;        }    case ItemStatus.Rejected:        {            statusLabel.Text = “Rejected”;            break;        }}//or alternatively just take the ‘name’ as [...]

Posted in C#, Refactoring | Also tagged | Comments closed

Refactoring below the surface. Things aren’t always as they seem

Continuing the refactoring from last week, I spotted another great example this morning that I ‘almost’ didn’t improve at all. My ‘entry point’ into this refactoring was trying to eradicate dynamic SQL and also the dreaded SELECT *, so this snippet popped up in my search.My immediate reaction was to refactor the dynamic SQL to [...]

Posted in C#, Refactoring | Tagged | Comments closed

Feelgood Friday – Refactoring with .NET Generics

As it’s Friday I thought – let’s do some cleaning up.  The following exercise is refactoring a class from an app I’m maintaining at the moment.  The app’s fine (functional etc), and was originally a .NET 1.1 build.  A combination of this, a previous, straight conversion to .NET 2.0 (with the minimum of mods) and [...]

Posted in .NET Framework, C#, Refactoring | Also tagged , | Comments closed

Efficient XPath Queries

This is something I get asked about quite a bit as I had a misspent youth with XSLT… One of my pet hates is people always using the search identifier ‘//’ in XPath queries.  It’s the SELECT * of XML, and shouldn’t be used unless you actually want to ‘search’ your document. If you’re performing [...]

Posted in XML | Also tagged , , | Comments closed