One of the best tips I’ve heard on class design is the following : Say the Class name and the member names out loud. If it doesn’t sound right, you’ve got to change something.
As a case study, let’s look at the Flex UIComponent. Ok it’s called UIComponent. Fair enough that’s a pretty good name. UI says user interface. It could be GUI which would be more precise, but as everything in Flash/Flex is geared towards graphics this can be forgiven. Component says that it isn’t just eye candy and that there is some functionality behind it, and is purposely vague, which is ok as it’s a base class.
Now let’s look at a few member functions and properties. The first thing that strikes you when you look at the doc is how many there are. That’s already a bad sign. I don’t think even the guy who wrote it knows everything that’s in it.
Next, there’s a whole lot of properties that have the word width in it. Width is a good fit for UIComponent, it feels like it belongs there. The obvious advantage of this is that when you look for the « width » property, it’s where you expect it to be. But how about the following :
ExplicitMaxWidth, explicitMinWidth, explicitWidth, maxWidth, measuredMinWidth, measuredWidth, minWidth, percentWidth. Does this not strike you as a bit too much ? Couldn’t there be some more elegant way to access all this width goodness ?
But my favorite is the following : determineTextFormatFromStyles. Why is that even in there ? Surely this could be in another class, maybe dedicated to styles or something ?
I’ll stop there. These are just a few examples of UIComponent overstepping its bounds. From a pretty clear cut idea and a good name for a class and concept the Flex team has somehow turned it into a monster with over 9000 lines of code. Maybe if they’d thought things through out loud things wouldn’t have got so far out of control.