How To Avoid Fear Of Adding Classes

See FearOfAddingClasses.

[ GradyBooch has been known to say "When things get too complex, add more objects." ]

For each class in which you want to add a new attribute (InstanceVariable) think again: If what you are trying to differentiate is that the object has a new set of possible states (see StateDiagram), usually you end up with states which do not transition easily into other states. This usually means that objects have different behavior based on their state.

Is behavior class-based or state-based?

If class-based, then create many small classes with many small methods, as behavior changes.

If state-based then you need one larger class with a lot of logic inside. Except that state-specific classes can be implemented with the StatePattern.

-- GuillermoSchwarz


Shouldn't you identify the cause of the fear first?

For me, in perl, the fear comes from the myriad ways to write the 8 or so lines of perl needed to create a proper, (inheritable,...) class. And then there are several ways to make attributes, and several more ways to make methods.

In Ruby, this is not a source of fear, because it's dead simple to create a new class.

And in a PrototypeBased language, it's even less of an issue!


Here's an experiment for helping to overcome the fear: take a substantial piece of code where there are a few classes with many methods and starting turning methods into classes. Start with the longer methods that have many local variables. The goal is not to produce an ideal implementation, but to explore the nature of objects.

EditText of this page (last edited November 10, 2014) or FindPage with title or text search