InfoQ

News

Partial Methods: Do They Belong in C#?

Posted by Jonathan Allen on Sep 06, 2007

Community
.NET
Topics
Programming
Tags
Code Generation ,
C#

One of the more controversial additions to C# is partial methods. A partial method is usually defined in a partial class file and is optionally implemented in the regular class file. If the partial method is not implemented, the compiler strips out all mention of it.

Partial methods are heavily restricted. They must be private, cannot return a value, and cannot have out parameters. These restrictions are necessary because any calls to a partial method that was not implemented are simply ignored. This in turn means it cannot be used as a definite assignment to a variable. Visual Basic also has partial methods and though VB does not require definite assignments for variables, it has the same restrictions.

With all these restrictions, one has to ask, "What are they good for?" Well basically, they are only used by code generators for a sort of lightweight event handling. As Alexander Jung explains

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. They are easier to declare and use than events (pay for what you need) and if not used they simply vanish (pay as you go).

This performance boost is not free. Because of the restriction that partial methods must be private, Alexander has uncovered these flaws.

The con: If you like metadata driven applications (and already struggled with ASP.NET databinding because there is no way to attach meta data)… well, prepare for some further loss of information. If you need some event for any property setter (for tracing or tracking), if you need any kind of dynamic behaviour (e.g. to attach some kind of generic rules engine), … let’s just hope the developer of the code generator anticipated that use case (or prepare for some work). You have a clean layer separation and the entities should know nothing about the UI? Well, put your code right into the data class will spoil that. But hey, you might use partial methods to implement real events. Manually.

Other people are likewise concerned about partial methods in C#. Most of these concerns come from the use of code designers. Stefan Wenig writes

First, because I'm not a big fan of designers. I'm quite worried they'll soon enough be sending us down the same road that COM-based development went in its last years, with hundreds of designers and wizards that generated ATL and MCF code nobody ever wanted to read. The Ruby crowd will be laughing their asses off with a few lines of source where we'll be stuck with designers and generated files and complicated build processes. (Compare that to COM/C++ vs. Java in the late 90s!) Wasn't C# about code-based developer productivity in the first place (vs. VB's designer-driven RAD road map)? You should be the last to care about the whims of the designer-based, enterprise-library-minded, software-factory-code-generator-happy folk. Resist!

Ayende Rahien is even less kind,

Let us corrupt C# for code generators: The horror story of partial methods

Code Generation & Architecture by Stefan Tilkov Posted Sep 6, 2007 12:41 PM
generation gap pattern... by andrew mcveigh Posted Sep 7, 2007 2:02 PM
Why always beating up on code-gen? by Mat Hobbs Posted Sep 9, 2007 9:02 AM
  1. Back to top

    Code Generation & Architecture

    Sep 6, 2007 12:41 PM by Stefan Tilkov

    We often apply code generation in our projects, following a light-weight MDE/MDA approach ... one of my personal rules has always been that the fact that you're generating code should not affect your architecture - if it does, something is wrong. Letting code generation affect your programming language seems even worse.

  2. Back to top

    generation gap pattern...

    Sep 7, 2007 2:02 PM by andrew mcveigh

    For code generation where I may need to add manual bits later, I've always used the generation gap pattern. Generated code goes in the base class, manual code goes in the subclass.

    www.research.ibm.com/designpatterns/pubs/gg.html

    A bit of a nuisance, but works well.

    Having said that, partial classes aseem useful to me for breaking a single class up into multiple files.

    Andrew

  3. Back to top

    Why always beating up on code-gen?

    Sep 9, 2007 9:02 AM by Mat Hobbs

    Code-gen is the perennial whipping boy but will always be with us, for as long as modularity and performance are still important.

    Is it trends from academia vs. the real-world? Academia has the voice but the real-world makes the money!

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.