Summary
Alexander Jung points out the risks with C#'s new partial methods, a feature added to the language mainly to aid code generators.
Advertisement
Partial methods, a new language feature in C#, has already generated some controversy. In a recent blog post, Alexander Jung summarizes some of the downsides and potential risks of partial methods.
In describing partial methods, Microsoft's Scott Wisniewski
notes that they are a light-weight replacement for events, and are primary designed for use by automatic code generators, such as those found in Microsoft's new LINQ designer:
[Partial methods] are declared by creating a private method with an empty body and decorating it with the Partial keyword. The method may then be "re-implemented" elsewhere within its containing class. If the method is implemented, then the compiler will redirect all calls to the partial method to the implementing method. If the method is not implemented in its containing class, then the compiler silently removes any calls to it from the program...
Wisniewski also contrasts regular event-listeners, such as those found in Java, and partial methods. One such difference is that partial methods are a compile-time feature, allowing the compiler to ignore unimplemented partial methods:
The wiring of partial methods ... is always done at compile time, and an implementing method is associated with its declaring method by simply writing a method with the same name and signature as the partial method. Once this is done, all references to the partial method are rewritten by the compiler to refer to its implementing method... If a partial method does not have an implementing method then the compiler will simply optimize away any calls to it...
As Microsoft increasingly focuses its efforts around code generators that aid in the development process, partial methods are an important link between a code generator and a compiler, and can help produce more effective generators. Yet, as many developers have noted, partial methods may make C# more complex for most developers. Alexander Jung, for example, points out in recent blog post, Partial Methods for Partial Developers, that:
The usual (probably the only relevant) use case for partial methods is light weight event handling in combination with code generation. Say, you parse a database or an XML file and generate data classes. Tens of classes, hundreds of properties, all fairly generic, boilerplate stuff. And also fairly common is the need to interfere with this method for validation or that property setter to update another one. So instead of manipulating the generated code or having hundreds of events and thousands of method calls during runtime (of which in most cases only a tiny fraction will actually do something) partial methods kick in...
Unless I enter the camp of the not-so-common-code-generator-writing-developers (it happens, but only rarely), I can see no relevant use case that allows me to employ partial methods. (I really don’t count the example of increasing the readability conditional compilation as relevant for a new language feature.)
What do you think C#'s partial methods? More generally, what do you think of adding features to a language to aid automatic code generators, even if the feature makes the language more complex for most developers?
If you have some pattern / repetitive behavior in your program, you should abstract it out with a function. If you can't do it, your language isn't expressive enough and you're stuck with code generation. But if you're going the way of modifying language — why not add expressiveness instead of code generation kludges?
> If you have some pattern / repetitive behavior in your > program, you should abstract it out with a function. If > you can't do it, your language isn't expressive enough and > you're stuck with code generation. But if you're going the > way of modifying language — why not add expressiveness > instead of code generation kludges?
Because that expressiveness is almost surely going to include some sort of dynamic behavior and that would be heretical.
> > If you have some pattern / repetitive behavior in your > > program, you should abstract it out with a function. If > > you can't do it, your language isn't expressive enough > and > > you're stuck with code generation. But if you're going > the > > way of modifying language — why not add expressiveness > > instead of code generation kludges? > > Because that expressiveness is almost surely going to > include some sort of dynamic behavior and that would be > heretical.
And that dynamic behavior, especially if it happens at runtime, might make your program run slightly slower. Choosing expressiveness of your source over speed in the executable: now that is heretical!
> > > If you have some pattern / repetitive behavior in > your > > > program, you should abstract it out with a function. > If > > > you can't do it, your language isn't expressive > enough > > and > > > you're stuck with code generation. But if you're > going > > the > > > way of modifying language — why not add > expressiveness > > > instead of code generation kludges? > > > > Because that expressiveness is almost surely going to > > include some sort of dynamic behavior and that would be > > heretical. > > And that dynamic behavior, especially if it happens at > runtime, might make your program run slightly slower. > Choosing expressiveness of your source over speed in the > executable: now that is heretical!
There heresy or lack thereof in such a view is entirely dependent upon the types of applications one writes. Now in this particular case, since one would not normally pick C# or any language compiled to any sort of IL for something that has performance as a critical requirement, I would agree with you. But there are so many exceptions to this that it is not something I would hold as a rule of thumb.
Personally, I would look at partial methods as something akin to operator overloading. If you find it useful and it can clearly and easily solve a problem, use it. Otherwise, don't. Just because the feature is there does not mean you have to use it.