Code faster with Unit Testing

Wait a minute! How can you possibly code faster when in fact you have to write more code? Test Driven Development requires additional (redundant?!) classes which only duplicates work. The “redundant” code has to be maintained and kept in sync with the main software. It might be good for some big companies (preferably at the other end of the world) but not for us. We all heard that.

There are many good articles explaining how great TDD is in a long run. Programmers are well aware of the concept and consider Unit Testing a good idea. Despite that many of them still don’t do it.
Unit Testing is little bit like losing weight. You need and want to do it but immediate pleasures are easier choice. Long term results might never come and require lots of discipline and sacrifices. Fortunately it’s not exactly as losing weight. With a tiny mindset shift you will realise there are in fact immediate benefits as well.

In this post I will try to explain how Unit Testing can improve the way you work. You will spend less time on counter productive and distracting tasks. In consequence it will let you code faster and better. It will also make you happier!

Adding a new feature for many years use to look like this:
– create a user interface
– start adding some business logic in your IDE of choice
– switch to web browser
– replicate user steps to see output of the new feature
– switch to IDE
– add some more code
– switch to the web browser
– replicate user steps to see output of the new feature
– repeat last 4 steps until feature is done

Switching between windows is a distracting exercise. You often get lost between many open programs, messenger and browser tabs. People try to mitigate that with two monitors or crazy screen resolution. It helps but doesn’t solve the problem.
Another and often bigger distraction is replicating user journey. Every time you make a change you have to go though the boring exercise of clicking on the same things over and over again. Sometimes it only requires to refresh a page. On some other occasion it’s more complicated. You might have a session which can expire if you code for too long. With the session you can lose some temporary settings like customer basket or payment options. Repopulating all of that changes records IDs in a database which lead to another problem and additional waste of time.

I think I made my point. It’s more productive to not leave IDE during programming.

To break this pattern test business logic with Unit Test. When code is done attach it to an interface and test integration. That can be done from a web browser. Usually one check is enough but sometimes minor adjustments might be needed.

It’s possible to run Unit Tests directly from most of the modern IDEs. My setup is Eclipse + PHPUnit. If you work on a remote server you can run it as an external tool which I described in previous post. If your develop on your desktop you can use a plugin. I’m using PTI-PHPUnit which works well for my needs. With or without a plugin the idea is to see output without switching windows.
To make it even more efficient use hotkeys. Using mouse to click a small button somewhere above the code can be distractive as well.

The last thing to cover is creating tests. While you have to create an individual tests for particular problems (which usually is 10-20 lines per a test) everything else can be automated.
Test classes have the same structure. You can use templates or snippets library to generate them. Repeating patterns like setting up application, authentication or inserting tests data can be packed into an abstract class or a Trait (PHP>=5.4).

If you haven’t tried Test Driven Development yet I strongly recommend it. You will be amazed how much time you can save by doing it the right way. It also liberates you from boring clicking and distraction (which also contributes to productivity!). Apart from the immediate wins you will get all the long term benefits. It’s the way forward.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s