Three classes squished into one
I was working with a class that held two boolean flags, and a little bit of conditional logic:
OutputFragment wasn’t bad, but the code that created instances of of it was:
Just… yuck. I could have used named arguments to make it more readable:
But that’s just lipstick on a pig. The real problem here is that there are three separate classes being squished into one. Let’s look at the possible values of the optional and overflow attributes, and what type of fragment they represent:
overflow optional type of fragment
F F required
F T optional
T F overflow
Splitting the class
So what if we actually make three separate classes, one for each type of fragment? Let’s see what that looks like:
In a language without duck typing, these would each derive from some interface or base class. With duck typing, and in cases like this where there is no shared behavior, there’s no need (and even in cases of shared behavior, mixin modules may be better than inheritance).
You might be thinking that the one class kind of exploded into a lot
code, and you’d be right. So what’s good about this? We did get rid
of the two boolean flags, and of the conditional in #peek
. But
the real payoff comes where the classes are made:
The removal of those boolean flags (and, in the case of
OverflowOutputFragment
, of the value
argument as well), makes this
code clearer, at least to me. What do you think?