The Artima Developer Community
Sponsored Link

.NET Community News Forum
Partial Methods for Partial Developers

5 replies on 1 page. Most recent reply: Sep 9, 2007 8:00 PM by Merriodoc Brandybuck

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Partial Methods for Partial Developers Posted: Sep 6, 2007 12:10 PM
Reply to this message Reply
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?


ju ju

Posts: 6
Nickname: juborland
Registered: Apr, 2007

Re: Partial Methods for Partial Developers Posted: Sep 7, 2007 12:45 AM
Reply to this message Reply
programming/designing is to be easier with code generators. But what if the language to use them is more difficult to handle?

Stanislas Klimoff

Posts: 6
Nickname: graven
Registered: May, 2007

Re: Partial Methods for Partial Developers Posted: Sep 7, 2007 2:33 AM
Reply to this message Reply
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?

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Partial Methods for Partial Developers Posted: Sep 7, 2007 8:41 AM
Reply to this message Reply
> 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.

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: Partial Methods for Partial Developers Posted: Sep 7, 2007 9:04 AM
Reply to this message Reply
> > 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!

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: Partial Methods for Partial Developers Posted: Sep 9, 2007 8:00 PM
Reply to this message Reply
> > > 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.

Flat View: This topic has 5 replies on 1 page
Topic: Microsoft Forms Team to Support Development of F# Previous Topic   Next Topic Topic: Anders Norås on DSL Development in C#

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use