Partial Classes and Methods – A Bit Ri dicu lous

The idea of a partial class was an idea brought on by Microsoft in the .NET framework awhile ago and ever since then I have seen very little use of them. However, I am sure they are around alive and well in more complex production code. I for one am not a huge fan of the idea of splitting a class over multiple files just for maintenance reasons, but I could see where it might be useful. What about this new idea of partial methods? They work similar to the partial classes, as one would expect, but I think perhaps this is going a bit too far. We will cover the topic on this entry of the Programmer’s Underground!

For those of you who are new to the idea of a “partial anything” the idea is relatively simple. Imagine that you had two code files in your solution. In one file you create a class called “Dohicki” and then add some methods and private variables etc. In the second file you also define a class called “Dohicki” but with some other methods and variables. Normally this would cause a problem because the compiler wouldn’t know which version to use. However, if you append the keyword “Partial” to the front of each class definition, .NET would take both copies and merge them together at compile time into one form of the class.

Sounds like a wonderful idea doesn’t it? The only problem I see with the idea is from a maintenance standpoint. What if you had a problem (a bug) in that class and you wanted to go fix it? You would have to track down all the parts of that class that could have merged together. A piece of the class is in one file, another in a second file, maybe a third or fourth file. It could easily turn into a nightmare if not organized properly.

I can see how this type of system might be useful however. If you have a great class that stands on its own, but then either you or another developer want to contribute some more to it, but without wanting to touch the original, you could make it partial and then “add on” your code. Personally I would see even this a great pointer to having inheritance, but that is just me.

Now to take this one step further, they have introduced partial methods. Where a designer can develop a method to a partial class but it too be labeled with the keyword “partial”. The designer would create just the signature of this method (no body code) and allow developers to come along and plug in custom implementations for that method. So when the two partial classes are merged, so are their partial methods to form a fully functional single method.

I for one find this somewhat of a bad idea for many of the reasons and I think the idea of partial classes could be a problem. From a classic system design perspective you are breaking apart your logical units and moving away from somewhat of a central modularized (proven) design. No longer do you have to go to one place to find the code, but you will have to code hunt to find the pieces.

Lets be a bit silly for a moment and assume that you had 10 partial classes which will be all merged into one whopper of a class at compile time. Now lets assume that each of these files had been created by different developers working together to form a game. Suddenly a bug appears in your character that when they are hit by an enemy their health goes negative rather than triggering some kind of death sequence. Normally in this situation you could go to the class that defines your character and figure out why their health is going negative and why no events have been raised to say that your character has died. You see that the method that would assign the life points is labeled partial and so is the class. After closer inspection of the actual implementation code you realize that nothing appears to be wrong.

You are now faced with two questions 1) Is there any other partial class with partial method floating around adding its own code? 2) If so, where would I begin to look? 3) Do I really have to dig through 10 files to find which one has the partial code that is causing the bug? This could become a major nightmare… especially if you are working with team members across the globe and can’t simply ask the office “Who added code to the damage subroutine of the character partial class?”

I understand the use of partial and how its strength lies in the fact that it would make great use of actual generated code, but at what point do we go breaking basic design principle in order to make code generation easier? Isn’t code generation the bane of programmers existence anyway due to its often bloated code?

Partial classes fine, but I for one think partial methods are a bit too much. Creating such mechanisms sets proven standard coding mechanisms back a bit. Every step forward we take, we end up taking two back with this sort of code structuring.

My overall advice, use them sparingly and make sure you plan out your files carefully if you are going to split code across them like this. Here would be a great place for labeling and be the most useful for others for finding all of the pieces when maintaining your code.

Thanks for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 20 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.