Friday, May 29, 2015

Simple Code

Despite the complexity of the situation, software should aim to provide a simple solution.
It may sound very simplistic but simplification is the most important skill required for problem solving.


Simple code has all or some of the following characteristics:

  • Does what it has to do and does it well.
  • Does what it has to do in a short number of steps.
  • Files and folders are well organized. (See my post on Consistent Code)
  • Classes represent only one thing.
  • Methods do only one thing.
  • It is readable.
  • It is consistent.

If you have not read my introduction to good code: "What is good code?", please do it. It will take you just a couple of minutes and will give you the context to follow this post.

How to simplify your code?

Usually software is compared to a tool, like a hammer or a screwdriver but software is also like a machine, composed of multiple moving parts and able to perform actions without human interaction.

Given this machine like behavior, we need to simplify our software objects and actions.



In general you could follow two different paths to simplify you code:

  • Top Down
  • Bottom Up

Sometimes, doing architectural changes, implementing new patterns and functionality into your code is the best way to go, but this is expensive and if you don't have unit tests it can be really risky.

Sometimes, what you need is to make the code more manageable and testable with a reduced amount of cost and little risk, ones you have done this, then you can think of something more complex.

Top down code simplification:

Complex objects are composed of simpler objects for instance a skate board:
  • Deck
  • Grip Tape
  • Trucks
  • Wheels
  • Bearings
  • Mounting Hardware


Actions are composed of simpler actions, for instance shaking hands:
  • Walk close to the other person.
  • Look at the other persons face to validate their identity.
  • Look at the other persons hand to validate height and position of the hand.
  • Get hand in front of the other persons hand.
  • Get your palm in contact with the other persons palm and wrap your fingers around the other hand.
  • Shake your hand.
  • Let go of the other hand.

In "Top Down Simplification" you are working with existing code not creating something from scratch, the goal is not to rebuild the system but to make it simpler, more manageable, more testable, better.


..it happens in all human affairs that we never seek to escape one mischief without falling into another. Prudence therefore consists in knowing how to distinguish degrees of disadvantage, and in accepting a less evil as a good.”
Niccolò Machiavelli, The Prince

Bottom up code simplification:

Bottom up is what you use when you are building a new piece of code. It requires a constant refactoring process.

The idea is very simple:

If a piece of code can be named then it deserves its own place.

Here you have a very simplistic example:  Shake Hands >>> Let go of other hand:

"Let go of the other hand"
  • Stretch thumb
  • Stretch index finger
  • Stretch middle finger
  • Stretch ring finger
  • Stretch pinky
  • Move hand against the direction of the palm
  • Move arm to the side of the your body
  • Stretch elbow
  • Relax muscles on the arm
  • Relax muscles int he hand

Could be turned into several methods:


Let go of the other hand
  • Open hand
  • Move arm to the side
  • Relax arm
                         Open hand
  • Stretch thumb
  • Stretch index finger
  • Stretch middle finger
  • Stretch ring finger
  • Stretch pinky
Move arm to the side
  • Move hand against the direction of the palm
  • Move arm to the side of the your body
  • Stretch elbow
Relax arm
  • Relax muscles on the arm
  • Relax muscles int he hand


Which is simpler to read and understand and more testable since you can validate the outcome of every method separately.


A scientific theory should be as simple as possible, but no simpler."
AlbertEinstein

The magic number 7:

An average person will not work well with more than around 7 plus or minus 2 items at the same time.

You can find out more at "The Magical Number Seven, Plus or Minus Two Some Limits on Our Capacity for Processing Information" by George A. Miller. (Harvard University)


The magic number 7 in code simplification:

Some people say that methods should have at most 5 lines of code. This is too idealistic therefore extreme and unrealistic.

I think that 7 actions (not lines of code) present a better measure.

Split the methods in three sections:
  • Input validation (counts as 1 action even if it has several lines)
  • Body (5 actions is excellent, 7 actions is good, more is too long)
  • Exception handling (counts as 1 action even if it has several lines)
The total will be 3 to 9 actions, 

This way an average person will not have a struggle to analyze your code.  



Simple does not mean easy, it takes practice, but the more you do it, the better you get at it.

It might never be perfect but it will be better then before, which is good.






0 comments :

Post a Comment