Unit tests are a vital part of emerging code design


Don’t limit yourself. Many people limit themselves to what they think they can do. You can go as far as your mind lets you. What you believe, remember, you can achieve.

Mary Kay Ash

When a developer writes code there is always an aspect of design to it.  Some developers spend more time on initial upfront design others like to start coding straight away.    The design and structure of code will emerge to a more efficient structure during the writing of the code, this process can be helped by writing units to assist the refactoring process.

Designing Code

I like to encourage developers to pause and think about the design of their code before they start writing code.  The benefits from developers thinking before coding

Think before you act

It’s quicker to change a design plan than design and code

This reminds me of the carpenter saying

measure twice and cut once

Thinking = planning

They are stopping to think about the task and creating a design is similar to looking at a map before you set off driving towards a destination.

  • A brief design gives you a plan
  • code designs emerge

You initial design will not be the finished design because designs are high level.  When you focus on individual parts of the design more questions and problems will come into view.

It’s like viewing something from far away, you see some general shapes and details.  The closer you get the more detail you see.

The role of unit testing in code design

Many people believe unit tests are written to catch bugs, it’s true writing unit tests could find some additional bugs you might have missed.

reusable tests for all developers.

It’s true unit testing has many benefits but here are the benefits to code design

Benefits of unit tests to code

  • Refactoring
  • Simpler code design
  • Code Documentation


the main benefit I have experienced with unit tests recently is the ability to refactor my code and run tests against the code to make sure it still works and hasn’t broken anything.

Simpler code design

When code is written to be tested the code design is simplified so testing the code is easier.  It’s easier to test code which does one thing and adhere’s to the single responsibility principle.

Code Documentation

Unit tests provide a working documentation of code.  Allowing other developers to easy see what the code should do by the tests and run/debug the code using the unit tests

This article (which is very good) THE ACTUAL COST OF TESTS – PART I, adds  additional benefits to unit testing

  • Code coverage
  • bugs in tests

The goal of code (design)

The way most developers work is they aim to get the initial code working and aim to tidy the code later.

This approach is driven by two factors

  • working code not matter how ugly can be shipped
  • The developer has proven their logic is sound

Primary requirements of code

  1. The primary requirement of code is deliver the functionality required

long term requirements of code

  1. Readable (expressive names, logically structured)(
  2. Modular/independent code to manage the effects of change (cohesive code, loose coupling)
  3. Reuse
  4. Easy to maintain/debug/enhance the code

The primary and short term goal of code can be achieved with minimal effort in the design of the code.  The code will deliver the required functionality but usually in spaghetti code with these characteristics

  • Big classes and Methods
  • classes\methods doing many things
  • high complexity
  • tight coupling

The above code is what I call Legacy code and developers have given up trying to keep this code manageable because the task of refactoring the project has become to big to contemplate.

An important aspect of refactoring is removing duplicate code, Martin Fowler has a great article on repetition.  Removing duplicate code has numerous benefits such as

  • Create reusable classes/methods
  • Reduces code
  • Better designed code

Unit Tests and refactoring

If you code has unit tests it allows developers to refactor the code with confidence because they can easily test the code still works by running the unit tests.

Unit tests fit in with the way developers write code (get it working first) but without unit tests the refactoring and structuring the code for the long term is  considerable more difficult because the developer would need to manually test the code is working.

Code designs emerge during the coding process, developers see more efficient ways to structure code which usually involves removing duplicate code, writing smaller more efficient code.

Code design can be viewed in similar way to writing a novel.  Novels go through many drafts and revisions whilst the writer removes unneeded sections and poorly written sections.  Some novelists can change the core structure/characters of books during revisions as a byproduct of investing more time focused on the task.

Code design is a similar process because it’s easier to restructure a working code design and improve a current design than it is to create a perfect code design first time.

Problems and efficiencies of code only come into view when a developer moves closer to the problem.

The key points

Without unit tests developers won’t refactor code to deliver the long terms benefits of well designed code.

Code and projects where code is not refactored reach legacy code status very quickly.  The developers who have to manage and work legacy code will take longer and longer to interact with the code (debug/enhance/extend/understand).

The end result is not writing unit tests will cost you more time and developers jobs are less enjoyable

12 thoughts on “Unit tests are a vital part of emerging code design

  1. Charles Wattson May 26, 2015 / 7:48 am

    The most important thing with unit tests is that it fits the way developer writes the code and yes it can become more difficult to refactoring and structuring the code without unit tests for the long term.

    The unit test cycle is like this : Write new test —> Run Tests —> Write Code —> Run Tests
    —> Refactor — |

    As developers write test case with the understanding the specifications first, you can expect robust design and development.

    So, With the unit tests developers will refactor code to deliver the benefits of well designed code.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.