Sunday, June 8, 2014

Do you favor Removing Duplication or Expressive Code?

Some time ago Kent Beck observed that there was a pattern for simple design


  1. Runs all the tests
  2. No duplication
  3. Expressess developer intent
  4. Minimizes the number of classes and methods
There has been some debate over the ordering of #2 & #3. Of course, this is just a observation of a pattern ordered by preference, personal preference, so it makes sense that you might have your own preference. But...
"What is your personal preference?"

It seems like that should be easy enough to determine, just ask yourself, but here's the rub:
cognitive scientist & economist have long observed that there is a great difference between 

Stated values:  what we say.
Demonstrated values: what we actually do.   

If we just ask people we will only get the stated values, to get what they actually preference we need to see what they do in action. For that we need a test.


Removing Duplication Test

Take a look at the following:
Let's remove the duplication. Just circle the duplication that you would like to remove then scroll down to see what your individual preferences are.










Answer #1

If your answer looked like this:


You would probably written a piece of code like this:
    iLike("fishing", "biking", "playing", "swimming", "sleeping");

and you have demonstrated a preference for:  #2 Expressive Code over #3 No Duplication

Answer #2

If your answer looked like this:


You would probably written a piece of code like this:
    iLike("fish""bik""play""swimm""sleep");

and you have demonstrated a preference for:  #2 No Duplication over #3  Expressive Code



Which did you preference?
  
pollcode.com free polls 



5 comments:

Rich Shealer said...

I understand what you are demonstrating, and I went with Expressive version. In the past I have done the Removing Duplication method and then later was annoyed that a change in a phrase forced me to re-expand the code. This was after the days when every byte counted, but old habits die hard. So I’ve gotten away from thinking or at least implementing that tightly for very small returns.

Looking at the second solution, my first thought was what happens when you have an activity that does not end in –ing, but when I couldn’t come up with one, I searched and found that all apparently gerunds end in -ing. So in this case it may actually be as okay to assume the -ing as it is the”very much.”

Unknown said...

It is possible to have valid duplication in the system. This is usually caused by the Single Responsibility Principle. Here is my reply blog post with my long answer

Llewellyn Falco said...

Rich - I agree and thought of this example
"playing video games"

as in

I like playing video games very much.

It's a bit of a stretch, but I personally like it when stuff work for things that are a bit outside the original plan.

Unknown said...

Separating the "ing" does not always work, it assuming the variable is a verb. Make it a noun and it no longer works. For example; "I like women very much".

Unknown said...

Removing the duplication doesn't mean going as far as illustrated in the #2. The example #1 is exactly as "DRY" as the #2 example.