views:

307

answers:

5

In C# 3.0 Microsoft introduced support for something called partial methods.

Do you use them? Can you describe how and why?

Do you consider the use of partial methods good programming practice, or not?


edit: Two people downvoted the question? I find that puzzling and amusing. Should I not be asking this?

+9  A: 

Partial methods are primarily useful for extension of the behaviour of tool generated code without cost in either runtime evaluation or user visible code where such extensibility is not used.

As such their use is sensible and to be encouraged where it is necessary, but such occasions will be relatively uncommon for most users (who are not writing tools to generate code). If you are writing such a tool then consideration should be given to where people may which to interact with the flow of your generated code, and whether such usage cannot be handled easily through event like mechanisms while achieving your intended performance and usability goals. Events are inherently multicast and such structure may be inherently against the intended design of the API. Alternatively a complex return value, or interaction with ref/out parameters might be necessary, finally the extension may be complex/fragile despite its utility and as such only the partial class implementer may be in a position to adequately handle this. All these reasons have their niche, if not being common and partial methods can effectively solve them.

Consumers implementing partial methods should use them as the tool generated code dictates (if an extension point is supplied and you need it, use it). To avoid doing this because one feels that the feature is confusing would be poor use of the language and API since this is clearly the intended extension point.

ShuggyCoUk
A: 

Microsoft programming culture has always favored a looser "duct tape" approach. This goes back at least as far as the Quick BASIC vs. Turbo Pascal wars. Microsoft languages allow shortcuts. Take, for example delegates: a delegate is basically an interface that you forgot to design in. Partial methods are just more of the same.

The title of your question refer's to Edsger Dijkstra's infamous letter to CACM and this is very apt. The Dijkstra letter was the official starting gun for the structured programming wars. Personally I don't think you will find wisdom by advocating either a strutured or an unstructured path in all situations.

I've always thought this conflict was a product of the differences in the engineering vs. computer science perspectives. Eary Microsoft programmers were programming bare metal all day. After you've written enough assembler, the dictates of structured programming seem a bit silly.

The tradeoff boils down to: would you like enough rope to hang yourself, or would you like to be forced to rewrite parts of your architecture to satisfy inflexibility of the language? A disiplined team can write good code in either environment, and there's no universal right answer.

Back to the quesiton at hand: I don't believe partial methods are a great language feature, but if they were necessary to make LINQ work, they're definitely worth tolerating.

Paul Keister
"a delegate is basically an interface that you forgot to design in. "This is entirely incorrect and I invite you to consider why microsoft didn't have to bother with anon interface implementations in c# compared to java and why lambdas are trivial to integrate into c# from 2.0 onwards. NOT Everything in the world is OO, stop trying to treat everything as a nail when all you understand is hammers.
ShuggyCoUk
Please fully read and understand the posters question before spouting off nonsense. Partial methods != Extension Methods. Partial methods were introduced in .NET 2.0 (before LINQ). Don't mix the two concepts and confuse others.
Foovanadil
A "delegate==interface" is actually an apt comparison, because from CLR point of view, a delegate type _is_ just an abstract class with some "runtime magic" that is there solely for the sake of optimization (just look at ildasm output for a delegate type definition). The "forgot to design in" part is clear trolling, though, since delegates are just as explicit and typesafe as single-method interfaces. Also, what "Quick BASIC vs Turbo Pascal wars" bit is all about? Among other things, there was Microsoft Pascal, and if there was a "war", it was between MSC and Turbo C. Rest is more trolling...
Pavel Minaev
Re: Foovvanadil's comments, that was my initial thought - *I wasn't asking about extension methods.*
Cheeso
@Foovanadil partial *classes* were introduced in 2.0 but partial *methods* were a c# 3.0 feature (perhaps why some people confuse them so readily with extension methods).
ShuggyCoUk
Perhaps this is a troll, but there was most definitely was a competition between Quick BASIC and Turbo Pascal. Look it up if you don't believe me. I'm not claiming either the Java or C# method is the correct approach, just that each has its pros and cons. It seems to me that the need to anonymous interfaces in Java goes directly to my point. It's about the tradeoffs of flexibility vs. structure. Go ahead and downvote me, gents, but know that you are easily trollable. When someone post a question ending in, "considered harmful," it's an invitation to prattle on, IMHO.
Paul Keister
The community would appear to disagree, or at least make your prattling _correct_. delegates have considerable inherent power that an interface does not, consider unbound delegates for instance methods or the built in multicast behaviour (see how many people implementing their own events in java get that right). flippant remarks which are factually incorrect tend to result in -1's
ShuggyCoUk
+1  A: 

I had occasion to use a partial method on a class library I wrote. It was possible to compile the library into one of several different versions, with the use of defined constants, that would compile-in or compile-out various blocks of function.

But littering the code with #if / #endif for all the combinations of options, cross with Compact Framework as well as desktop framework, led to some confusing stuff.

I used partial methods to sort of simplify that piece - as sort of invisible or implicit #if/#endif. This is similar to the way they're used in LINQ, as I understand it.

On the other hand I don't, at runtime, add in these methods, as LINQ would, or does. Rather than the linq model, where there are separable assemblies, and when combined you get extra function, in my class lib, there is a single DLL built for each combination of options. This is to make deployment and consumption easier.

Cheeso
I think you may be inferring a connection between partial methods and extension methods. None exists. Extension methods and partial methods are both compile time constructs true but partial ones are compiled in or not on the basis of whether the user bothers to supply an implementation, extension methods are just (sweet, sweet) sugar to a static method call and have no conditional aspect.Incidentally the conditional compilation you refer to is pretty much exactly what partial methods do under the hood, just automatically based on what you do in a separate file...
ShuggyCoUk
I don't think I am inferring that connection. I understand the distinction completely.
Cheeso
"On the other hand I don't, at runtime, add in these methods, as LINQ would, or does" what do you mean by this then, are you referring to conversion of an Expression<T> into a function? partial methods can never be added at runtime (well unless you dynamically launch csc or its equivalent)...
ShuggyCoUk
Linked in. Partial methods can be linked in, or not, right? I don't do that.
Cheeso
partial methods are not 'linked in' they simply compile to the method call (which is a straight non virtual call) or absolutely nothing.If you are *always* implementing the partial method and it would be an *error* to not implement it I would suggest you are abusing them since you will not be informed by the compiler that you haven't done the right thing...
ShuggyCoUk
A: 

Partial classes make C# and VB work more like C and C++ where you can scatter the code that implements a class across different files as you see fit. Their main purpose is cleanly support visual designers for things like WPF. I consider this a good use for partial classes.

Another use I have seen is to split up a large source file into logical parts. For example class BigForm might span the BigForm-BillingInfo.cs, BigForm-ShippingInfo.cs, and BigForm-LineItems.cs files. This is usually a poor use of partial classes, because refactoring into multiple classes or controls is better OOP design (for reusability, etc.).

binarycoder
partial classes are different from partial methods.
Cheeso
@Cheeso: EXACTLY! I downvoted it.
Robert Koritnik
A: 

Partial classes are a very useful feature, for example, when you have a client and a server and both of them need to share data types for business object persistence. You have your regular class server side, you expose it client side with your web service and if you want to decorate things client side, very beautifully, you can make a partial version of your class (client side you have a code generated version of the server version through proxy generation etc.).

I just wanted to say this example, as I make an extended use of partial feature, nowadays, working on a silverlight project, persisting disconnected objects :)

//Sorry for not noticing that the question refers explicitly to methods. Partial methods are very useful for weaving code functionality in event handlers, auto generated in certain scenarios.

Aggelos Mpimpoudis
But as pointed out above, partial classes and partial methods are two distinct things.
Cheeso
OOOOOOO... my bad! :(:(:( However I see partial classes and methods highly useful and not at all harmful, confusing, or security holes (as someone had said to me sometime :P). Their concept is simple: allowing multifile class + method definition, whether it is for code generated elements or not :) (thx for the comment and sorry for not noticing the "method" in the question....)
Aggelos Mpimpoudis