Saturday, June 07, 2008

Another reason why architects should write code

Johanna Rothman explains why architects should write code. I completely agree.

Not only does hands-on involvement reduce risks on the project, it also reduces embarassment when talking to your kids ;-) Here's an actual conversation with my daughter:

What did you do today Dad?
I drew some diagrams. Do you know what a diagram is?
No
It's a picture that shows how something works. I drew some diagrams, then I talked with some people about them.
So you didn't do any work then?

Wednesday, April 30, 2008

Only 4 New Features in C# vNext?

I've always admired the effort that Microsoft put into the design of C#. It's difficult to find the right balance between power and flexibility, and on the whole I think they've done an excellent job.

At the same time, it's been great to see the language grow, particularly with the addition of LINQ. As Jon Skeet has written recently, C# today is noticably more complicated than it was at version 1.0. Personally, I don't think that's a bad thing. The C# community has grown up along with the langauage, learning the new features as they have been released.

I can see this process continuing for some time, with the language continuing to grow. So I was suprised to see this comment from Microsoft's Mads Torgersen, in response to some suggestions that I posted at Microsoft Connect:
"We have to do some harsh prioritization, both because of our implementation and testing resources, but also because we need to keep the number of new langauge features at a manageable level - depending on how you count, we are adding only four language features to C# this time around".
We more-or-less know that one will be some form of dynamic lookup. That just leaves three others. They might be big "killer" features (like generics and lambda expressions in previous versions) but there's still only three of them.

I don't know what to think. Should we be trilled that Microsoft are continuing their excellent track record of keeping the language simple, or should we be dissapointed that there will be so few improvements?

Saturday, February 23, 2008

Summary of C# vNext Ideas in this Blog

Here's a summary of the ideas I've suggested in this blog, for future versions of C#:

Enhanced Automatic Properties

Recently there's been a lot of blog posts discussing things people would like to see in the next version of C#. Here's my addition to the wish-list:

"Automatic properties that let me supply a method body." It sounds like a contradiction in terms - but it doesn't have to be. Instead, it would be a compact syntax for defining properties that use standardised method bodies (where you define your own "standard" bodies).

Some places where it would be useful:
  1. Cases where many properties follow a standard format.  For instance, all the properties generated by the LINQ designer could be single-line automatic properties.  (Much less verbose than the current approach).
  2. ORM's and similar products that require properties to be written according to certain conventions.  (E.g. Mindscape LightSpeed)
How would it look?

Right now, we can write automatic properties like this:

public int Foo { get; set; }
My suggestion is that the words "get" and "set" could be replaced by the names of methods. For instance:

public int AdvancedFoo { GetValue; SetValue; }


Where the methods "GetValue" and "SetValue" are generic instance methods with these signatures:

/// <summary>
/// When passed the field, return the property value
/// </summary>
T GetValue<T>(ref T field)
{
// ... your own code goes here...
}

/// <summary>
/// When passed the field, by ref, and the new property value, set the field
// </summary>
void SetValue<T>(ref T field, T value)
{
//...your own code goes here...
}
Given the above property, "AdvancedFoo", the compiler would compile it as if I had written this:

public int AdvancedFoo
{
get { return GetValue(ref _advancedFoo); }
set { SetValue( ref _advancedFoo, value); }
}

private int _advancedFoo; // compiler-generated backing field
Just as in today's automatic properties, the compiler would generate the backing field for me. The difference is that it reads and writes that backing field via methods which I define.

This goes a long way towards giving the best of both worlds: the compact syntax of automatic properties, and the flexibility of my own code (plus standardisation - all me properties that are supposed to work the same way do work the same way, because they all use the same code).

Questions:

Why does the "GetValue" method take the field "by ref"? SetValue needs it by ref, but why GetValue? GetValue takes it by ref simply so that you can use this trick to find out which property is being read - just in case you need to know. This is something you are more likely to need to know when setting, e.g. to raise change notificiations, but maybe some people want to identify the property in GetValue too. Using the ref param makes that possible.

What type should the complier use for the backing field? Simply put, it should use whatever field type is implied by the field parameters of the "GetValue" and "SetValue" methods. E.g. an automatic property for a LINQ EntityRef might look like this:

T GetEntity(ref EntityRef<T> field) where T:class
{}

void SetEntity(ref EntityRef<T> field, T value) where T:class
{}
allowing me to write this:

public Customer Bar{ GetEntity; SetEntity; }
and have the compiler compile it as if I had written this:

public Customer Bar
{
get { return GetEntity(ref _bar);}
set { SetEntity(ref _bar, value);}
}
private EntitySet<Customer> _bar;

Note that the compiler is able to infer that the backing field should be EntitySet, not Customer, based on the signatures of Get/SetEntity.

(And how would Get/SetEntity actually work? That's a question for another day ;-) I do have some (working) prototype code...)

What do you think of this idea for enhanced automatic properties?

Update 2 March: posted on MS Connect here (in the comments)

Update 12 March: And here here as an MS Connect item in its own right.

Thursday, February 14, 2008

Extension Methods - Feedback to Microsoft

If you've been following the disussions about extension methods in this blog, you might be interested to know that I've submitted the issue to Microsoft's feedback site. Jump over there and vote for it, if you'd like to see something done.

Friday, February 08, 2008

Better Property Change Notification

aka Automatic INotifyPropertyChanged

Some time ago I developed a new way to do property change notification. It consists of code which can automatically figure out which property has changed, without you having to tell it.

Details are here.

I've now put together an updated version, and posted it on CodePlex here.

Here's how to use the new version:

Option 1:

Here's the easiest option...

Add this code to your class or base class:

PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();

public event PropertyChangedEventHandler PropertyChanged
{
add { _propertyChangeHelper.Add(value); }
remove { _propertyChangeHelper.Remove(value); }
}

protected void SetValue<T>(ref T field, T value)
{
_propertyChangeHelper.SetValue
(this, ref field, value);
}
Then write your property setter methods like this:

public int Foo
{
get { return _foo; }
set { SetValue(ref _foo, value); }
}

Option 2:

If you want more control, you can find which property changed like this (and then do whatever you like):

protected void SetValue<T>(ref T field, T value)
{
...
field = value; //Actually assign the new value
PropertyInfo changedProperty = PropertyMap.GetProperty(this, ref field);
// do whatever you like, now that you know which prop was changed
...
}
Key differences in the new version are:

  • No "trial sets". The old version used to call all your property setters, to figure out which fields they corresponded to. That could be annoying, because you ended up recieving calls to the set methods when your objects were not necessarily ready for them. The new version doesn't need to do this. Instead, it inspects the MSIL of the set methods, to figure out what it needs to know. (The inspection is a one-off runtime operation, so it won't be a performance problem.)

  • Fewer contraints on how classes are coded. The old version required that you nominated a special field as the "reference field", to which all others were compared. The new version does not require that. Also, the new version lets you use your own base class (with your own SetValue method, named whatever you want). Fianlly, you can put your own code in the property setter methods (along with the call to your "SetValue" method), something which could cause problems in the old version

  • Helper class for easy implemetation of property change notifications (as above)

  • Granualar implementation, so that you can call some of the implmentaiton classes, but still write the outer "layer" of the API yourself if you need to

  • Lock-free. The new version is threadsafe without explict locks (and without and [ThreadStatic]s). Update 10 Feb: This used to rely on a little trick with generics and static fields; but that doesn't play well with inheriance. It now uses a class I wrote called LazyDictionary, which is basically a dictionary with double-checked locking to ensure that locks are only requested during initialization, and not during the on-going running of the application. (Didn't want a lock request on every property set call!)

  • No separate C++ assembly. The old version used managed C++ to do stuff that C# cannot. The new version uses reflection emit, to produce the corresponding routine on the fly, so that the separate assembly is not required.



Finally, I have resurrected the name "ActiveSharp" (which I used for a previous, abandonned project) for this new version of the code. I did that for two reasons, firstly the new version borrows code from the old ActiveSharp (particulaly the MSIL inspection); and secondly I liked the old name and couldn't think of a better one ;-) Just like the previous "ActiveSharp", this new version is a product of my microISV, Tasman Logic Ltd. (The microISV is not exactly a money-making venture, given that its only product is open-source ;-)

Monday, January 14, 2008

Resolving Extension Methods

Paul Stovell (author of SyncLINQ) has pointed out another issue with Extension Methods. As he writes:

The problem is the methods are *resolved* at compile time.

Suppose you had this:

void DoStuff(this IFoo foo);

class FooFactory
IFoo Create()

If your factory returns an object that provides its own instance implementation of DoStuff, it WILL NOT be invoked - the extension method will instead. This breaks many of the rules around polymorphism and good OO in general...


In other words, the compiler favours instance methods over extension methods, but only if it can "see" the instance method at compile time.

The reason is simple: currently, resolving an extension method is just a compiler trick, so compile time is the only time when it CAN be done.

But Paul's comment got me thinking. It occured to me that, if my idea about using interfaces was implemented, then it would be possible to properly solve the problem he described. It would work like this:

First, to recap: I suggest that there should be a mapping between extension methods and interfaces. Think of a set of extension methods as an "extension contract". You can make calls on the methods in that contact either by calling intstance methods (on objects which implement the interface) or by extension methods (for objects which do not implement the interface). This is to solve the versioning problem, but it can also be used to solve the problem Paul described.

How? I'll try to illustrate with an example. Imagine that our "extension contract" is called IExtensionContract. It contains two methods, Foo and Bar. We can call Foo and Bar on objects which don't support IExtensionContract, simply by calling them as extesion methods. But, when we call Foo and Bar on objects which DO support IExtensionContract, we want to call the version that is on the object even if we can't tell that the object supports them at compile time. (That's the heart of the issue, right Paul?)

That could be done if the complier worked like this:

Given this code to compile

ISomething s = ...
s.Foo();

The compiler should compile it as if the programmer had written

ISomething = ...
IExtensionContract c = s as IExtensionContract;
if(c == null)
StaticExtender.Foo(s); // invoke the extension method
else
c.Foo(); // invoke the instance method

This would be possible, if the interface-based versioning solution was adopted, because that solution introduces just enough runtime information about the extension methods - namely the "contract" to which the methods belong. Contrast that to the current situation, where there is no runtime information about extension methods.

Here is the user-authored (i.e. not compiler-generated) code for the example above:

public static class StaticExtender : IExtensionContract
// "inheriting" from interface here is my suggested addition to the language
{
public static Foo(this ISomething s){...}

public static Bar(this ISomething s){...}
}

public interface IExtensionContract
{
void Foo();
void Bar();
}
Finally, all the suggested logic I have blogged about previously still applies. The compiler still needs to decide, at compile time, whether the call is to a method in the extension contract or to a like-named (but otherwise unrelated) instance method. All that logic still applies as I described it here.

Saturday, January 12, 2008

Readability in Validation

My favourite pattern for validation code is to have one method per validation rule. I think it makes it easier to read and manage the rules.

For instance, I like to be able to write something like this:

[Validate]
protected bool EndDateMustBeAfterStartDate()
{
return EndDate > StartDate;
}
I like to refine the pattern a little, in order to:

(a) Construct error messages based on the method names. That makes the code more concise and encourages good method names. E.g. the failure message from the rule above will be "End date must be after start date".
(b) Allow you to, alternatively, specify dynamically-generated failure messges from within the method body (see below)
(c) Easily specify if the rule is not applicable in a particular situation (again see below).

I've attached some sample code (with tests).

By the way, I once wrote some code to fire rules automatically, whenever a property used by the rule was changed. But that's not included here. In this sample, you have to ask the system explictly to run the rules at a particular time.

Here are some example rules, and the code to call them:

public class Animal
{
...
public string AnimalName
{
get { return _animalName; }
set { _animalName = value; }
}

public int NumberOfLegs
{
get { return _numberOfLegs; }
set { _numberOfLegs = value; }
}

[Validate]
protected Result NumberOfLegsMustBeEven()
{
return NumberOfLegs % 2 == 0;
}

[Validate]
protected Result MythicalSpeciesAreNotAllowed()
{
if (AnimalName == "Unicorn" || AnimalName == "Dragon")
return Validation.Error("'" + AnimalName + "' is not a real species");
else
return Validation.NotApplicable;
}

// Note the use of the OR operator here to give a compact syntax for returning a custom error
[Validate]
protected Result NumberOfLegsMustBePlausible()
{
return NumberOfLegs <= 10 || Validation.Error("Your '" + AnimalName + "' has too many legs");
}

...
// execute like this
foreach(Result r in Validator<Animal>.ExecuteRulesOn(myAnimal))
if(!r.Pass)

// or, like this, if you don't want to use the generic Validator class
Validator v = Validator.GetInstanceFor(typeof(Animal));
foreach (Result r in v.ExecuteRulesOn(myAnimal))
...