23. May 2010 05:19
The development team I work with have designed and developed a few large projects recently and we've definitely learned quite a bit from each of them. Many of us work in different locations, have different skill sets, program in both VB.net and C#, but work on the same projects. Based off my personal experience, I'd like to share some tips that have helped me create better software. Any development team could benefit greatly from some simple practices that will increase code reuse, improve standards compliance, increase productivity, and make it easier for team members to maintain each other’s work. This in turn improves the overall quality of our software.
- Project Structuring
- Create separate folders for each programming language, C# and VB.net (and of course others if you use). To do this for multiple languages, you need to add a codeSubDirectories node to the compilation section of your web.config file.
- Below that, create sub folders for each file’s/class’s scope. i.e. a “DataAccess” folder for data access, a “Business Objects” folder for business objects, a “WebServices” folder for Web Services. This will make it easier for team members to locate a specific file/class.
- Namespacing should be used and explicitly declared in each class the team develops. The namespaces should clearly describe the classes function and be grouped according to its role and project. This makes it so much easier to find what your looking for in a large project, not to mention looks nice and clean in the object explorer. For example:
- Inheritance and Basing
- Repetitive properties and members should be placed in a base class and marked MustInherit(VB.net)/Abstract (C#) to promote reuse. Good candidates would be a "DALBase", "PageBase", and "ErrorLogBase".
- One instant and no brainer benefit to centralizing your database connections in your "DALBase" class is the headaches it saves during the applications life-cycle and maintenance. One simple change effects how all of your data access code hits the database.
- Implement IDisposable
- In classes that use managed resources implement IDisposable and do all cleanup in the dispose function.
- This will improve memory leaks, close any open datareaders, and close any open database connections, etc.
- Data Access Layer
- The DAL should never represent database columns as an ordinal, i.e. “myDataRecord.GetString(43)”. Columns should always be represented as the textual name of the column i.e. “myDataRecord.GetString(myDataRecord.GetOrdinal("CustomerID"))”.
- The DAL should not contain any validation, this should be done by the Business Object.
- Business Objects
- Business objects should be created for frequently used tables and should all be designed in a similar fashion.
- No database activity should occur in the Business Object.
- Business objects should simply be a proxy to the DAL and contain validation when setting it’s properties. If the validation is invalid and you haven't created custom exception classes, then at a very minimum throw an exception of ArgumentException(“xyz value is not valid, must be a positive integer”) .
- Business objects should be validated before sending it to the DAL.
- I recommend creating a streamlined approach in which each field in the database has a corresponding property. Each Business Object would have one “Save()”, “Delete()”, and shared(VB.net)/static (C#) factory “CreateItem(pass in primary key) as ClassBeingCreated” method.
- Debug with Firefox and Firebug add-on
- Firefox is more standards compliant than IE and provides a better, which provides you with an immediate view of how compliant your HTML/XHTML is. Chances are that if it renders correctly in Firefox, it will also render correctly in IE, but the opposite is hardly true. Obviously testing in IE should still occur. In case you don't know how to do this, here's a post from ScottGu on how to change the default browser VS uses for debugging: http://weblogs.asp.net/scottgu/archive/2005/11/18/430943.aspx
- The Firebug add-on for Firefox is absolutely invaluable and a huge time saver for client side debugging especially AJAX.
- Change !DOCTYPE declaration from the VS2005/2008 default of XHTML Transitional to XHTML Strict
- The XHTML Transitional !DOCTYPE is meant more for transitioning legacy applications from VS2003 or earlier. If you are starting a new web project, best practice recommends you use Strict.
- This will force you to comply with XHTML 1.0 Strict standards and write more compliant XHTML. Remember, XHTML promotes separating presentation from content so many simple tags like “align” and “center” are not supported. Instead XHTML requires you to use CSS. Here is an excellent resource in determining which tags are not supported in XHTML strict: http://www.blackwidows.co.uk/resources/tutorials/xhtml/attribute-comparison.php.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- VB.Net Options
- I prefer that Option Explicit and Option Strict explicitly be set to “on” in each class. I realize this can be set globally in web.config, but declaring it explicitly in each class leaves no question as to the authors intention.
- Read these two awesome books
I'm hopeful folks find these tips useful. They're simple, but practical and will help you develop a better solution.