Please add sugar first (and some testable code)

Why do I have to write all these tests? It’s more code! What’s the point?

The amount of code in a comprehensive unit test might equate to the amount in the production module that’s being tested. So why double the code?

Think about that application you just built. It contains a myriad of features, and a few are particularly complex. How long would it take you to parse through the code base, find that one spot you need to test, and then proceed to implement it? Think about it. Your day would go far smoother simply writing out some extra lines of code that target your test specifications.

But wait. There’s more.

Sure, you can start a project by writing the functional code first, and slap on those tests at the very end. However, doing so risks creating code that is harder to test due to structural problems such as dependencies and modules organized in ways that would prove difficult when those tests start to run. Think of testing as the sugar you need to add when making some delicious brownies. If you never add in the sweet stuff, no amount of toppings is going to make for that tasty core. It works the same way with those assertions. The entire blueprint of the code changes when you start the development process with a test driven mindset.

At this point you might ask: What exactly is code that is “hard to test”?

It’s actually quite easy to spot. Let’s say you are writing a unit test for a particular module. The difficulty of writing that unit test will depend on the structure of the code. Does the module contain a giant function with multiple degrees of functionality? Are there heavily nested logical operations that utilize different private functions and variables? At some point, you begin to lose control of the code.

Yes, you can still manage to write that unit test. But how clean will it be? How clear and concise will your assertions look if the subject of its operations is a kerfuffle’d code base? A test that is hard to read is essentially useless.

This is why it is important to have tests baked into your code, and not added in as an act of desperation. It is important to understand that the very act of writing tests while developing changes the mindset of how you present the code.

You may be a fresh-off-the-boat developer, or a veteran with decades of experience, and writing tests can seem daunting at first. But never fear. In the end, testing is a skill like any other skill. It’s going to take a lot of practice, but it will lead to better code, cleaner tests, and a bunch of happier developers!

 

Advertisements

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 )

Google+ photo

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

Connecting to %s