Allen Buckley's Blog
My experiences in .Net

Common Issues found when Refactoring

Tuesday, September 08, 2009 9:31 PM

I’ve been doing a lot of refactoring lately and I’ve decided to list out a couple of common issues I found with code that I’ve come across in several projects I have worked on. 

One of the most common themes I’ve seen in ASP.NET projects is Business logic code in the Click Events.  It is really frustrating to see several hundreds of lines in one Event.  Please do not put ANY Business logic and especially no Data Access code in there!  There are several reasons why, but the biggest reason I have come across is that you really should be setting up Unit Tests in your projects.  All unnecessary code in the Click Event makes it much more difficult to write effective Unit Tests as these tests should be testing small pieces of functionality.  You end up having to go through and move the code into a Business Layer and and then refactoring code into several small logical methods.  You’ll be spending way too much time moving the code around just to create a test.  I guess this is the price that is paid when you try to implement tests in a existing project that have none.  At least there is one benefit…  You’ll get to test out your Unit Tests on all of the code you had to re-architect.  ;)

The other issue I keep seeing that I just touched on is methods with hundreds of lines of code.  Really, c’mon.  Didn’t we all learn in school to refactor our code into smaller methods?  It is much easier to find bugs with smaller logical methods.  If they are small enough and your method names specifically define their function you should be able to look at the call stack and almost instantly know what went wrong.  Throw a exception handler into the small methods and your troubleshooting of the bug becomes that much easier.

Inconsistent Naming Conventions is another thing I’ve noticed.  Teams should really decide to a convention and stick to it.  But, usually they are just too busy or don’t care enough and you end up with code that has a different style because every Developer that touches the code uses their own style.  So, you end up having to go through the code and conforming it to a convention 1 variable at a time.  This is can be really frustrating as the variables are usually in 3 or 4 different files and you could be doing this for hundreds of files.

It always comes down to writing good code versus lack of time to do so.  We all know what usually wins out, but doing things right the first time just prevents so much work after the fact.  But, this is nothing new.  It’s the story of Business Software Development.


Feedback

# Choose - travel to 4 star hotels England.Try luxury cruises

Choose - tour to 4 star Florence hotels, 4 star hotel Prague, 5 star hotels Ireland.Luxury cruises.Buy air line ticket now. 4/15/2010 1:05 PM | Baron

Post a comment