Tuesday, February 20, 2007

C# and Partial Classes

As I was perusing the Spacewar code-base I ran across an interesting class declaration:

partial class SpacewarGame : Microsoft.Xna.Framework.Game
{

Interestingly, I had apparently found part of a class. Being the diligent person that I am, I immediately set out to find the rest of it. Unfortunately, my search was in vain because this was the only declaration of the SpacewarGame class. I immediately wondered what benefit was bestowed by the partial keyword. A quick wikipedia search reveals the following:

A partial class, or partial type, is a feature of some object oriented computer programming languages in which the declaration of a class may be split across multiple source-code files, or multiple places within a single file.

My first thought is why is this actually needed? Apparently partial classes come in handy when dealing with generated code. The classic example is when dealing with windows form code. The generated code is hidden from the developer and she can easily add to the class without worry of losing those changes if and when the class is re-generated. Hmm... that sounds fair.

However, it apparently does not stop there! Another valuable benefit includes multiple developers working on the same class without stepping all over each other since they will be working on different files. This would seem to work especially well where locking strategies are implemented with version control software. Or, how about facilitating the modification of big classes? Ok, now I am starting to feel uncomfortable. In my view, both of those situations are a smell of bigger issues.

But I am told that I should not be concerned. The compiler collects all the class pieces and stitches them together, ensuring that a valid type can be constructed. In terms of maintenance, if I were ever unfortunate enough to work on a project that over utilized partial classes, I could rely on the Visual Studio Class view for easily accessible, complete class definitions.

Well, I am not convinced. Even though partial classes may have a use, in my view it is somewhat limited. I do see, however, that the ability to abuse them is quite large. I feel that I encountered the beginnings of that in the Spacewar code-base. In the end, I see this as another example of a well-intended feature making developers lives harder rather than easier.

On a side note, Ruby's mixins have been compared to partial classes, however I think they are fairly different animals.

If you want to learn more about partial classes check out the following links: msdn magazine , msdn reference

3 comments:

Zygote said...

Welcome to the world of XNA :)

I hope to see more from you and the Zen Garden :)

Ziggy
www.ziggyware.com

Daniel De Aguiar said...

Thanks Ziggy! I plan on being an active member of the community.

xmlblog said...

I've not used VS2005 RTM. However, in a very early beta I played with the thing that really irked me was that VS made it exceedingly difficult to locate the code that had been generated and swept under the rug of the partial class.

Mixins probably are the closest analog of this feature in Ruby, though the Ruby object system behaves totally differently, particularly when you take open classes into account. While I like Ruby's approach better, it can be equally dangerous. Jim Weirich has a great presentation on playing nicely with others that lists some best practices that minimize the problem of abusing Ruby's open classes.