« Goodness, Gracious, Great Balls Of Plasma!| Main | How Big Should Your Class Be? »
Partial Classes. Totally Stupid?

Is .Net getting feature bloated?
Is it just me, or is partial classes the dumbest thing to come out of Redmond in a long time?
I was giving a class last week on Microsoft .NET and UML. The purpose of the class was to show folks how that, even if you put business logic in your UI layer, eventually with good programming practices you end up creating a tiered architecture.
So here I bring up .NET, start a simple VB form, and run the reverse-engineering tool. BLAHOOEY! There are two freaking classes on the diagram. Both have the same name. Ah. Thank you Microsoft. How to explain two boxes that are the same class to my students?
I made it through, of course. The pupils that were ahead of the game realized it was due to partial classes, and the rest just kind of got along. We ignored one of the boxes and that helped some, but that's really not right -- those UI components are really inside my form class, and it was important for me to show how they added complexity.
Since then, I've been thinking about partial classes, and aside from making Microsoft's products LOOK better, i think they are the worst thing I've seen in a language in a long time. Let's review, shall we?
Partial classes are set up like this:
public partial class foo
{
your code here
}
Then later on, somewhere else in your project, you can have something like this:
public partial class foo
{
MORE of your code here
}
That way, part of your class can be in one file, and part of your class can be in another file! But the fun is just beginning, folks. You can have as many of these little constructs as you like. Your class "foo" can be scattered all over your project -- the UI, the busines layer. Heck. As far as I know, you can include partial classes from external libraries. (maybe not so sure, but with the Code DOM you certainly can contiue to add onto your foo-monster as much as you want. It's everywhere! It's everywhere!)
Does anybody besides me see a problem here? And please, don't give me the old "bad coding practices are the responsibility of the developer, not the language provider". That's total BS. A good language only allows you to cut your foot off with power tools if the power tools are powerful enough to justify the risk to the developer.
Looking at this feature. Huh? As best as I can figure, we have partial classes to make the IDE UI look cooler. Oh joy. Instead of seeing some UI code that us heathens are not allowed, now that magic code is hidden away from the rubes, er, novice programmers. I'm sure a similar benefit is realized in the web pipeline. (although I can hear MS Architects complaining now. Bring it on, guys)
I read on one blog where somebody was praising partial classes because "different programmers can work on different parts of the class in isolation" WTF? I couldn't read this without laughing. The whole purpose of having a class in the first place is having all the stuff related to one thing in one place! By splitting it up into infinite little tiny pieces, they have taken the entire idea of classes and made a complete mess out of it. Now instead of having properties and methods and hidden code in one spot for you to check out and modify, you can have it in 15 pieces. That's a big improvement, all right. I can't wait to get the pleasure of checking out piece 7 of 16 to change one method. Hey Jim, Sue, and Tom! Are you guys finished with those other 7 pieces? Because we got to do unit testing. Somehow.
One guy said that partial classes were good to "separate the UI from the user-added code" LOL! Give me a break! That's what architectural tiers are for (which is what I was teaching the class on) Perhaps now we can have a tiered architecture inside a class? That will be a lot of fun.
Look. I can understand why software companies have to keep adding features into their code to make regular releases. It gives the appearance of making progress, and you can't have four hundred developers sitting around playing Donkey Kong all day. Got to give them something to do. Let's not forget the bottom line, guys! That's why, for instance, that you can launch an MX Nuclear Missile from one of the spelling menus inside Microsoft Word. But please, guys, don't make the same mistakes in your development platform! I don't want to wake up sometime in the future and a platform that does so much it does nothing at all very well.
If you've got a good reason we need partial classes, I'd like to hear it. I've thought about it a lot over the past week, and to me it looks like the language disaster from hell. If .NET keeps getting bloat like this, it will be just another C++ before the decade is over: a language where you can do everything so many ways that it becomes so easy it is extremely difficult to do well.
My two cents only.
I aggree that partial classes are open to misuse like multiple developers working on the same class. That is just pushing the limits. But the usage in VS IDE is a really good one. I don't want to see generated UI code in my "code behind". More important than that it supports the goal of seperating UI design with writing code behind UI which has been a push of Redmond in recent years. So "ideally" designer can take the partial class, modify it and have the UI developer plug it to the app. I know it sounds like a hack and I have yet to see that usage but hey it is there if you want to use it. Other scenario is code generation. Again I haven't use in that scenario either but if you are using code generation for data-access you can generate a partial class and keep the custom logic in another partial class and don't worry about the loss of changes.
And of course if you don't like it just don't use it. If you have to maintain a code that uses it in a wrong way that's why refactoring tools exist.
Hi Murat.
Thanks for your comment.
I had forgotten about code generation. That's odd, since the code generation tool I own uses partial classes!
I think you have made the best case possible for partial classes. However, I still do not feel that the architectural trade-off is worth it in a language. If setting up a form creates some UI variables and event sinks, I want to know about that. Splitting it up like that really doesn't separate the UI and Business code, as whenever you start taking events it happens in same part of your class as everything else. And as I explained, you don't want to use partial classes as a way of tiering. That sucks.
Even the code generation example I find lacking. If I wanted to use some base attributes from a code generation tool and then add to them, I would use inheritance. As I remember the discussion on my code generation forum (www.NetTiers.com), they wanted to use partial classes because it was "new" and "cool." This is EXACTLY what I was talking about -- programmers skipping design questions because of a new language feature.
I can certainly see that some folks like the new shiny bling. To me, however, it's just not worth the trade. I can understand people feeling otherwise. Perhaps there are other examples that would be worth it.
As a web developer the implementation of the partial class in a web project is a real hell, I don't matter seeing the ui code generated since I always like to make modification to it. If you toggle to see all hidden files inside VS 2005 for windows app works fine because you can see your class1.design.vb file, But where is it in a web project???? >:(,
Hi Guillermo.
Funny you should mention that. I was just creating a sample VB.NET web application for a client today when the VB.NET compiler lost all of my event sinks. No problem, I thought! I'll just go to the constructor and add the "With events" stuff.
Not happening! I guess Microsoft don't want us mere mortals honking around with those variables anymore.
Like I said, did they make it simpler? Or did they gain some kind of marketing blurb at the expense of adding complexity to the suite? I vote for the latter.
I've got a big web project we just ported from VS2003 to 2005, and I've been searching for my designer code for a while. I actually broke down and did a text search of the folder in windows, and I'm still not finding ANYTHING. What am I doing wrong?
According to this site http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/169009160831 the designer code no longer exists. I'd have to disagree. Although I still can't find it, I know that if I copy and paste a button, no matter what the name is of the button, they both link up to the same event. If that's automatically generated, it needs to be specified somewhere... It doesn't just remember things I do like that. WHERE IS THE DAMN CODE?!
And here's the solution: It's in the actual HTML source now.
<asp:Button ID="btnAddCause" runat="server" Text="Add Cause" OnClick="btnAddCause" />
There it is. Happy Birthday.