Simplified Syntax for Properties


 

When I first read about properties in C#, I was a bitexcited and a bit confused. I was glad to see so many inventions, includingproperties, designed to help programmers be more productive. But on the otherhand, why do properties require so much typing just to declare a variable? Andwhy does it seem we need to create two variables to do the job of one?

For review, this example shows the minimum syntax to createa property in C#:

/////////////////////////
private int myval;
public int Myval
{
   get
   {
      return myval;
   }
   set
   {
      myval = value;
   }
}
/////////////////////////

In my opinion, the problem with the above code is thatprogrammers must declare two identifiers, which are confusingly similar: myvaland Myval. You have to remember which one of the two you are allowed to usefrom outside the class. Of course, for a newbie, it's even more confusingbecause a third identifier, "value", is used without even beingdeclared. Okay, so you quickly learn that "value" is a keyword, notan identifier. But still, it should be easier than this.

Here is a proposed alternative syntax that accomplishes thesame thing and is also extensible:

/////////////////////////
public int property Myval
{
   get;
   set;
}
/////////////////////////

The new "property" keyword declares that Myval isan actual variable definition. As a property, the variable is only accessiblethrough the provided getter/setter, which are automatically generated in thiscase. By giving us an automatic, albeit simplistic, getter/setter, it's likethe compiler is saying:

"If you cannot afford a getter or a setter, one will beappointed for you." I recall the C language standards committee came upwith that bit of levity when they adopted function prototypes. Well, it's aboutas humorous as we can expect standards bodies to be.

Getting back to the subject, you can see that the simplifiedsyntax reduces the amount of code required, and the number of confusinglysimilar identifiers from three to one.

Well, sort of. We can say three to two, at least. The freesetter provided by the compiler, ummm, isn't very valuable. In fact, we may aswell just declare a regular variable as have a property with a free setter. Soit's true that the value keyword is still required to implement a real-worldsetter, but this new style is safer because the class author may notinadvertently violate the variable range rules by bypassing the setter fromwithin other member functions. In other words, having one identifier instead oftwo avoids the risk of the class author assigning an inappropriate valuedirectly to myval (not Myval) from other code within the class.

You may say this new syntax seems unusual. Hey man, what doyou think everybody says the first time they see a for loop in C/C++/Java/C#?As practical as the for loop is, it also proves the point that the syntax of alanguage rules! So just as the syntax of an enum is different from that of a struct,the property keyword is similar in that it also establishes special rules forthe syntax that follows within the braces.

The automatic (free) getter/setter in the simplifiedproperty syntax can be easily extended by replacing either or both with afunction, as C# currently requires. But either way, you still have the optionto avoid creating an unnecessary identifier for this common programming task.For example:

/////////////////////////
public int property Myval
{
   // usually we don't need a special getter, soone will be appointed for us
   get;
   // if we need a special setter, we do it withoutan extra variable
   set
   {
      if (value > 10)
         throw new Exception();
      else
         // within the scope of aproperty, the compiler "knows"
         // that assignment is not arecursive call…
         Myval = value;
   }
}

/////////////////////////

I hope the C# community will speak up about this, andfurther refine the concept, so Microsoft and ECMA will consider this for futureversions of the language.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!