Auto properties : What ifs ...
Posted
Friday, March 28, 2008 1:40 PM
by
bill
Paul Vick posted a speculative post as to Automatically Implemented properties for VB10. Although it's kind of nice, it really is just a minor modification from what C# did in 3.0 and misses a lot of the "what if" scenarios we should be asking.
What if :
- You want to add a break point on the property set or get?
- You need to add some validation or logging ?
- You want your existing code to look consistent ?
What's being proposed falls down on all those counts. A better approach, IMO, would be to just auto collapse properties, similar to what Refactor! does. That works with existing code1, and addresses all the what ifs above.
I also expect there will be IDE issues around the syntax Paul proposes as it is no different from the opening line of a Property today. So when you hit enter at the end of that line, you'd expect code completion. So adding lines between these auto properties will potentially break code.
And when you do press enter, let's assume it does complete the code block for you: it would need to include the backing field otherwise it risks breaking code elsewhere. That's a major change from today, where unless you use a snippet, you only get the property stubs.
What I would like to see, is allow the backing field to be declared inside the property block, such as :
Public Property Name() As String
Dim m_Name As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Then the property truly can be collapsed and expanded as it is fully encapsulated.
I'd also especially like us to be able to specify the default prefix used for backing fields when generated, be it m_ or _ or m etc. We shouldn't have to change our coding styles to match an inflexible code generator.
I think this feature needs to be looked at in terms of existing code, and code maintenance, not just a quick fix for writing pass through properties.
1. There's a bug in Refactor with properties in a Module, where it hides the property, not the backing field.