Red-Green-Refactor

Red > Green > Refactor– the test-driven development “mantra” used by developers that focuses on writing only the code necessary to pass tests. The concept of this development style is to 1) reduce errors caused by incorrectly guessing that an implementation will behave as expected, and 2) reduce bugs caused by adding new functionality to existing code. In delivering better designed and cleaner and more extensible code I have decided to share my thoughts and findings on some of the challenges and benefits afforded by this development style.

What is TDD?

TDD is a software development process that relies on a short iterative cycle:

  1. Red – write a failing test that implements a new feature.
  2. Green – write just enough production code to make the test pass, fake it if necessary but remember you will need to refactor this later.
  3. Refactor the production code to improve the design until it has reached an acceptable standard.
  4. Repeat until there are no new tests or the design is optimal.

RedGreenRefactor1.jpg

Understanding TDD and what to test

TDD_BDD_DDD1.jpg

At KRS we prefer nUnit for our desktop and web API C# Visual Studio projects, as well as a combination of Chutzpah, Jasmine or QUnit, and PhantomJS for our JavaScript web apps.

We also use tSQLt to test database logic.

If the software is written using stored procedures containing the domain logic, then tests should be written at that level.

Resharper is a great productivity tool and especially useful when refactoring.

A Good test…

What to avoid

Dealing with legacy code
Legacy code has usually been written without tests and it’s difficult to write tests on code that hasn’t previously been written with testability in mind.

TDD1.jpg

The benefits from TDD are huge and have enabled us to write simpler and better designed code. Being agile means frequent code changes and refactoring; TDD helps us keep quality up during these iterations and ensures very little or no rework once the code goes live.

About the Author:
Paul Johnson is the Technology Director as KRS. Paul has been with KRS for 15 years. When he is not overseeing the software quality and technological direction at KRS, he can be found at home creating delicious food or driving his “Scooby”.

Authors Notes:
Aslam Khan is well known for his contributions in the Agile community and corporate IT in improving software development. He both introduced and coached us on TDD and is currently collaborating with KRS on our latest Advanced Agile Developer course offering as well as designing Single Page Web applications.

 
0
Kudos
 
0
Kudos

Now read this

Excel: Dynamic Summing To-Date

Here is our example spreadsheet: The SUM formula in cell B15 is shown. It uses MONTH(NOW()), but could just as easily use a Named Formula: =SUM(OFFSET(Totals2009,0,0,ThisMonth,1)) Here’s the formula in Name Manager: How it works OFFSET’s... Continue →