Feed on
Posts
Comments

Those of us who live up near the front of the test-driven development (TDD) revolution have long been aware that TDD actually shapes the design. In other words, a hardcore TDD’er will produce a significantly different design than a non-TDD’er, though both are solving the same problem.  In particular, using microtests to advance the code creates designs with significantly more classes, each one having fewer responsibilities.

Is more and smaller classes good or bad?

Why?

Back in the middle fifties of the last century a fellow named George Miller wrote a magnificent paper called The Magical Number Seven, Plus or Minus Two: Some Limits on our Capacity for Processing Information.  Miller’s paper presents some highly suggestive evidence that the human mind has a channel capacity of about seven independent entities at a time.

Equally important, the paper introduces the idea of ‘chunking’, a means by which our brains can effectively evade this limit. Chunking is taking some of the entities in your channel and grouping them together to form a new entity .  Once a chunk is successfully formed, if can then be used as a single entity again.  This technique can be repeated over and over, thus the demonstrated human ability to manage systems containing even tens of thousands of independent parts.

I recommend you read the paper.  In addition to its relevance to writing software, it’s also just plain good writing.  But I realize that some of you work for a living, and feel you don’t have time for just sitting around reading articles, however important, from fifty years ago.  I’ll just cut to the chase:

Five classes that each do one thing is better

than one class that does five things.

Why should this be true? Using Miller’s chunking notion, we can provide a reasonable explanation.  Here’s a quote that’s damned close to being directly understandable in the software development domain:

There are many ways to [create chunks], but probably the simplest is to group the input events, apply a new name to the group, and then remember the new name rather than the original input events.

Imagine you are working with a modest but real ‘god class’. Let’s say we’re talking about 500 lines of code. It’s clear from Miller’s idea of channel capacity, this is way over the limit of what humans can readily manipulate.  When we break the code out into a bunch of 20-line methods, we still have at least 25 entities involved, so no love there.  That’s okay, we can make one more push and segregate these methods into five subsets, roughly correlating to  responsibilities.  That’s just a slow way to say our class does five things.

Now, Miller says five chunks is probably ok. But the chunking job that you did is only implicitly present in the code, so there are some features of this situation that are sub-optimal.  Because the chunks in this case are only implicit in the code:

  • you spent time performing the chunking, e.g. identifying the chunks;
  • you have created a valid chunking, but you still don’t know if it correlates to the author’s intent;
  • even if your valid chunking matches the author’s intent, it may not actually match the problem you’re trying to solve;
  • and finally, if you don’t break the ‘implict chunking’ situation, you guarantee that you and others will encounter it over and over again.

order generic levitra If you have a sudden decrease or loss of desire, affects some women at certain times in life such as pregnancy, times of stress. Obvious thing. viagra no prescription canada or other medication has nothing to do with sexual desire. If you buy 20 pills you pay $72.60 US dollars and there is a saving of $7.20 http://acupuncture4health.ca/contact-me/ buy viagra online US dollars. Hotels generic viagra sale Miami South Beach, Miami Attractions and Everglades Tours – Reserve Online Now.
Breaking classes into smaller classes makes the chunking explicit.  One need not be a mental giant with a channel capacity of 50 entities in order to work with the resulting systems.  And one need not have to guess about the chunking that was originally in the author’s mind.  One also captures one’s own chunking by doing this, so that the next person to look at the code, be it you or someone else, can start in a significantly better situation.

Write More & Smaller Classes

By The Way:  Write More & Smaller Functions, Too

Comments are closed.