Geeks With Blogs
Jonathan Starr's Blog Thoughts on C#, Ajax, WCF, LINQ, Agile et al.

improve my => 'code' Add to Google

In the latest release of Orcas, one of the new features provided is "Automatic Properties" which allows developers to use a shorthand like the following:

    public class Person {
    
        
public string FirstName {
            
get; set;
        
}

        
public string LastName {
            
get; set;
        
}        
        
        
public int Age {
            
get; set;
        
}
    }

and the compiler knows to interpret this as

    public class Person {

        
private string _firstName;
        private string 
_lastName;
        private int 
_age;
        
        public string 
FirstName {

            
get {
                
return _firstName;
            
}
            
set {
                _firstName 
= value;
            
}
        }

        
public string LastName {

            
get {
                
return _lastName;
            
}
            
set {
                _lastName 
= value;
            
}
        }        
        
        
public int Age {

            
get {
                
return _age;
            
}
            
set {
                _age 
= value;
            
}
        }
    }


Prima facie, this looks like a good thing.  The developer is typing less code, and still has hooks in property accessors to perform validation, or needed side effects from value changes.

But I have a few problems with this shorthand.

Criticism 1

Why is this shorthand needed when there is a snippet (prop) that provides full property accessors, with better hooks for inserting custom actions like validation?

Criticism 2

I think that the shorthand looks too much like the signature of a method normally found in an interface, not a class.  The only difference appears to be an access modifier. So I find the shorthand syntax is confusing...


What do you think?

Jonathan Starr
Posted on Tuesday, January 15, 2008 8:00 PM C# , Critique , Software Design | Back to top


Comments on this post: Automatic Properties in C# - A Critique

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
As http://geekswithblogs.net/robp/archive/2008/01/14/another-reason-to-avoid-simple-properties.aspx point's out you can't debug this 'auto-props'. Which is really a pitty!

Personally I like the snippet way more.
Left by Marcus on Jan 16, 2008 4:15 AM

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
Yup, another vote here for the snippet or you could use the refactor menu option:

private string m_AProperty;

Press Ctrl+R,E at the end of this line and you get:

private string m_AProperty;

public string AProperty
{
get { return m_AProperty; }
set { m_AProperty = value; }
}

A lot quicker than typing the stuff for the automatic properties and it's more future proof - like you said, when you want to add validation you're gonna need to type the whole lot out anyway.

Does anyone know of a valid reason for automatic properties even existing - I'm sure there must be one. MS aren't the best at implementing features that are universally requested, so I can't see them going to the trouble of implementing one that isn't.
Left by Carl on Jan 16, 2008 8:10 AM

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
automatic properties, type inference, etc etc were all included to make what LINQ can do much easier and in some cases even possible. hey were exposed because it was felt that they could be handy was of accomplishing certain tasks that otherwise would be difficult. It was never intended that you use automatic properties all the time, only when the situation called for it.
Left by Jay Glynn on Jan 16, 2008 8:26 AM

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
Three reasons:
1) less names used - you only need to think of how to name the property, not the corresponding variable
2) Less visible code means programmers will waste less time on reading it, and that improves efficiency of code reviews and such.
3) If you wanted to remove a property before, you had to delete it in two places - now you only have to bother with one.
Left by SinsI on Jan 17, 2009 4:00 AM

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
I agree wholeheartedly. In my opinion a lot of the new supposedly cool shorthand features really make the code LESS readable which means LESS maintainable. I am back working on a product that is still selling after 15 years. Trust me, you don't REMEMBER what the code does. You need to be able to read it easily. Heck with this and heck with lambda expressions. Linq is also less readable in general IMHO.

Really who cares if the developer needs to type a couple extra lines? Don't write unmaintainable code. It doesn't make your job any more secure. AT ALL.
Left by John V on Jan 29, 2010 10:37 AM

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
Victory was short-lived, friendship is long! We must learn to treasure!
Left by ugg outdoor slippers on Nov 09, 2010 12:12 AM

# re: Automatic Properties in C# - A Critique
Requesting Gravatar...
WRT the comments on universal acceptance and future proofing, the best supported method would be the old school original way:

private data1;

public GetData1() {...}

because the original .NET style get and set

private data1

public Data1 () {
get{...}
set{...}
}

are shorthands themselves, which could very well be scrapped in a future .NET platform.

This newer style is a shortcut on a short cut, the most dangerous of all within the context of forward compatibilty.
Left by Samus Arin on Apr 30, 2012 9:01 AM

Your comment:
 (will show your gravatar)


Copyright © Jonathan Starr | Powered by: GeeksWithBlogs.net