This week marks my first inspection of the Component UI Application Block, or CAB for short. The motivation for looking at the CAB is that we’re preparing to do some enterprise level application development, and we wanted a framework in place that would allow us to write consistent code with a standard architecture. It’s also a good excuse to download the Visual Studio Beta 2 and dig into some of VS’s new features.
I’ve barely dug into the CAB – I just finished the first of seven labs today that the CAB developers have graciously provided. But it wasn’t the CAB that caught my attention today. It was C#’s new partial keyword.
Right off the bat I think this is a bad idea.
Very quickly in Lab #1 we’re introduced to the partial keyword:
If you are unfamiliar with the .NET framework 2.0 beta, you will be wondering what the ‘partial’ keyword is doing within the class declaration. You may also be wondering why there is a call to the InitializeComponent method, but the InitializeComponent method is nowhere to be seen!
Hmm… What’s that make you think of? Basic with Goto maybe? I thought we were evolving programming with first, Structured design, and later Object Oriented design with Design Patterns. This feels like a huge step backwards. Now we’re not encapsulating objects anymore (at least not at the physical level, even if the compiler is smart enough to assemble all these partial classes into one object), but we’re splitting them up.
Ask yourself: why do we make classes in the first place? It’s not for the compiler. The compiler couldn’t care less how our code is organized. Be it spaghetti Basic or well-structured OO, the compiler doesn’t give a damn. We do though – we encapsulate as much for ourselves as for any other reason. So that we humans can understand the logical layout of our code, and our application architecture. So that we can break complex systems up into discrete, reusable components that make sense and are easy to understand. Object Oriented design evolved because human beings write code, not machines. We encapsulate for ourselves.
And now we’re taking that thing of beauty – that self-contained object – and breaking it up into separate files. Ugh.
The Lab writers go on to say:
This is very handy, because not only can you save cluttering, but you can also implement your UI plumbing in one file, and your event handling in another. You can send your UI off to your visual designer who will create the front end, and your business logic and event handling code to your business logic developer, who is not interested in how the form looks.
Handy? Save ‘cluttering’ at the expense of consolidated code and physical representations that mirror their logical ones? How cluttered are your objects exactly? I think the first question anyone should be asking here is, “Why are your objects so cluttered that you have to break them up physically?”
And as for the UI, aren’t events part of the UI? If you put a button on a form or web page, it needs to do something. Even if you have a Graphics Designer who knows nothing about code building UI’s with a tool like VS, then you still need someone to wireup those events. You still need a programmer to step in and do something.
Now, someone is going to say, “Yeah, but if you have partial classes, then you can have the Graphics Designer and the Event Programmer working on the same class and the same time, in different physical locations.” My response is: this is one reason why we have pair programming – so people with specialized skills can share the wealth of their knowledge, and reduce errors at the same time. Do you want the Graphics Designer and the Event Programmer working separately at the same time on partial pieces of the same class, each creating their own set of bugs and mistakes? Or, would you rather have them working together, sharing knowledge, communicating intent and design to each other, and eliminating bugs? And what about complex UI components that have to talk to each other as part of their behavior? (reference: Gang of Four’s Mediator pattern)
The more I think about Partial Classes, the more I dislike what they do and why they do it. But the good thing is we don’t have to use them if we don’t want to.
Still, I can just imagine the horror new developers are going to have to deal with when they join a team that has implemented partial classes in their design. They’re going to take a look at a method call and wonder, “Where the heck is that implementation?” And, “Why don’t I have a Goto pointing me to its location?”