Design Guidelines for Constructors

I personally consider the 'Design Guidelines for Developing Class Libraries' to be one of the greatest documents ever written in the history of mankind and I'm fairly certain it's prevented numerous countries from going to war.

It gets some things so right sometimes, I just want to kiss the screen. Consider, "...use the same name for constructor parameters and a property, if the constructor parameters are used to simply set the property. The only difference between such parameters and the properties should be casing."

I know it's picky but you would be surprised how many times I see code like this:

public class Employee
{
    private int employeeId;

    public int EmployeeId
    {
        get
        {
               return employeeId;
        }
        set
        {
               employeeId = value;
        }
    }

    public class Employee(int x)
    {
        this.EmployeeId = x;
    }
}

In my opinion, it's not just the little, icky-picky things that make your code more elegant. It's taking all these guidelines into consideration.

Wait until your Intellisense prompts you for that 'x' in the constructor. Wait until you have to F12 into the constructor to remind you what the 'x' is for. In this wonderful world of Intellisense, abbreviations like this just aren't necessary. I'll admit I abbreviate like crazy when I'm prototyping and banging out test code. But I always go back and refactor the names to something more meaningful. If not for me, then at least for the next guy.
6/7/2008 | Comments (0) in .Net | C#
Email

Related posts