April 25, 2009

Clearing out the junk drawer


This is the first post in a series that I’m going to write while refactoring the BlogEngine.NET source to better match my own personal coding style. I’m going to be dogfooding my changes, publishing them to this site as I go.

Before I start I want to make it clear, though, that I have a lot of respect for the developers of BlogEngine. The changes I’m going to make and the comments that go along with them are not meant to be negative criticisms of their work. Rather, I consider this an exercise in expressing my coding styles. As I change things around, I mean no disrespect to Mads or Al or any of the other contributors. They’re doing a great job.

Having said that, one of the first things I noticed about the organization of the source code was the existence of a “Utils.cs” file. I’m always very wary whenever I see such a file, because often it functions like a junk drawer, collecting all of the miscellaneous functionality that doesn’t fit in any other obvious place. That’s not necessarily bad, but it can easily balloon out of control, with much of the commonly used core functionality being stuffed in this one file indiscriminately. This can make it hard to see where various pieces of functionality exist, as they all get buried together in the same drawer.

In this case, the Utils.cs file contains functionality ranging from string manipulation (RemoveIllegalCharacters, RemoveExtraHyphen, etc.), to sending email (SendMailMessage, SendMailMessageAsync, etc.), to password hashing and semantic discovery and code assembly discovery.


One of the first things I did was to begin separating the various type of functionalities into individual classes that are more single purpose. This more closely follows the basic concept of the Single Responsibility Principle. For organization, I decided to place some of the extracted classes into a folder called “Agents”, with classes named TextAgent, MailAgent, and so on.

For me, this forms a much better organizational structure for some of the commonly used “utility” functionality.