Back Draft


Cleaning House

Your new .NET code can be faster, stronger, and better.


By Jonathan Goodyear


The release of Visual Studio .NET 2003 has got me thinking. Many of us have been developing .NET solutions for well over a year now. At this juncture, it is probably a good idea to take a look at what you have accomplished so far with .NET, and see if there are any ways to improve upon it.


Generally, the initial code you produce with a new development platform is not as good as the code you produced with the old platform. This is because you are not as intimately familiar with all of the new platform's features, tricks, intricacies, and nuances. Now that you're a bit more comfortable with your "not so new" .NET surroundings, apply your experiences to streamline the code you wrote when you were just starting out with it.


For example, when I first started building ASP.NET Web applications, I wired up each server control to its own event handler. It was several months later when I had one of those "light bulb" moments and figured out you could wire multiple related server controls to the same event handler:


Private Sub Button1_Click(ByVal sender As System.Object, _

  ByVal e As System.EventArgs) _

  Handles Button1.Click, Button2.Click, Button3.Click

    Dim id As String = CType(sender, Button).ID

    'do something based on the ID of the button that

    'generated the event

    Select Case id

      Case "Button1"

      Case "Button2"

      Case Else

    End Select

End Sub


In C#, you can accomplish the same thing by using multiple delegate assignments to the same event procedure. Event aggregation minimizes code repetition and improves the quality of your code.


Another enhancement I made to my code recently is making more frequent usage of class constructors. Constructors weren't available in Visual Basic prior to .NET, so I was not conditioned to take full advantage of them. My Visual Basic roots also taught me that setting state in business objects is not a good idea because of the thread pinning that results. The .NET Framework takes care of these threading issues for you, so passing initialization state into an object at construction is not the performance bottleneck it used to be. Effective use of constructors makes your code more concise and your method calls less cluttered with repetitive parameters.


There are also a couple of features specific to the new version of the .NET Framework that can help you clean out your code. The IDataReader interface in ADO.NET now supports the HasRows property, making it easier to use the lightweight DataReader classes (SqlDataReader, OleDbDataReader, and OracleDataReader) to determine whether rows were returned.


A nice improvement to Visual Basic .NET is you now can declare variables in a loop declaration. This feature (available previously in C#) comes in handy when you need only a variable within the context of the loop itself. So instead of having to use this code:


Dim item As Order

For Each item In OrderCollection

  'loop code

Next item


you only need to code this:


For Each item As Order In OrderCollection

  'loop code

Next item


You can imagine now many single-use loop variables this feature alone could eliminate.


The bottom line is that whether you realize it or not, both your .NET skills and the evolution of the .NET Framework itself have come a long way in the last year or so. The learning curve for .NET was particularly steep, given its sharp break from traditional Microsoft-based development frameworks, so take some time to bring your fetal code up to par with the high-octane .NET development skills you now possess. The hard work and shell shock are over. It's time to start producing quality code that is both highly maintainable and stretches the limits of what .NET can do.


Jonathan Goodyear is president of ASPSoft (, an Internet consulting firm based in Orlando, Fla. He's a Microsoft Certified Solution Developer (MCSD) and author of Debugging ASP.NET (New Riders). Jonathan also is a contributing editor for asp.netPRO. E-mail him at mailto:[email protected] or through his angryCoder eZine at


Tell us what you think! Please send any comments about this article to [email protected]. Please include the article title and author.




Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.