Shorthand Properties = Lazy Programming, IMHO
During an in-class demo of some code I had to edit (and that was inherited from a couple classmates), I mentioned that I really disliked some of the code my classmates had written. Namely, I didn't like their use of shorthand properties. C# lets you do something like this:
public type Name{ get; set; }
I don't like that. To me, it violates the encapsulation aspect of object-oriented programming. Encapsulation (in a nutshell) means you "hide" the attributes of your class, and limit the access of external code to those specific accessors you create. In C#, we can easily encapsulate by making our member variables private and then create public properties.
Now, with shorthand property syntax, you can skip creating member variables. The property itself becomes the member variable.
Let me repeat what I said earlier: I DON'T LIKE THAT.
So one of the guys I inherited this code from asked my why I don't like that syntax. I explained my idea of encapsulation (pretty much verbatium of what I wrote above) and also made a few more points.
My biggest beef with shorthand properties is that you can't perform any validation on the input value. The only guarantee you have is that the data is type-compatible (since otherwise the compiler will not build).
I much prefer standard syntax, which lets you do something like this:
private int? majorID;
public int? MajorID
{
get { return majorID; }
set
{
if (value.HasValue)
{
if (value > 0)
{
majorID = value;
}
else
{
throw (new Exception("'Major ID' must be greater than zero."));
}
}
else
{
throw (new Exception("'Major ID' must contain a value."));
}
}
}
As you can see, I can do a little bit of error handing very easily with standard syntax. To me, that is more than worth the small amount of extra typing involved.
What do you think? Let me know...
:]
