Write Code Easier with TDD

Riri Edwina
4 min readApr 4, 2021

Have you ever write a test before a code? Does it sound magical when you are testing a code before it even exists? Well, say no more! Let’s get to know with Test Driven Development (TDD). It is an iterative process where the developer writes tests to validate their code. TDD is written based on user requirements. The developer has to make sure all the code passed the test they’ve made.

In short, Test Driven Development is a methodology when we develop a system by ensuring that every component in the system runs properly.

How do TDD works?

TDD has an iterative cycle. It consists of 3 states, they are Red, Green, and Refactor.

Red State

In this stage developer will initiate the test, the test must be failed. It consists of what expected from the application. All the requirements such as the functional and the UI requirements should be converted to test code at this stage. For example, I am working on a project, I make the UI of the project. Then I should have to write the test based on the existing design and because I do the project with my friends, I should push it to my git. This is how it looks like:

Here is a case of making a test that does not allow negatives numbers. The negative number will be thrown an exception case and here are some example codes:

@Test(expected = RuntimeException.class)public final void whenNegativeNumberIsUsedThenRuntimeExceptionIsThrown() {
StringCalculator.add("3,-6,15,18,46,33");
}
@Test
public final void whenNegativeNumbersAreUsedThenRuntimeExceptionIsThrown() {
RuntimeException exception = null;
try {
StringCalculator.add("3,-6,15,-18,46,33");
} catch (RuntimeException e) {
exception = e;
}
Assert.assertNotNull(exception);Assert.assertEquals("Negatives not allowed: [-6, -18]", exception.getMessage());}

Green State

In this state, the developer will implement the code based on the test. It doesn’t have to be an effective code, it was written only to make the test pass. For example, I write the code to make the test passed, and here is the commit:

Here is the example of the code on the green stage, it consist of the code of the program that needed:

private static int add(final String numbers, final String delimiter) {int returnValue = 0;
String[] numbersArray = numbers.split(delimiter);
List negativeNumbers = new ArrayList();
for (String number : numbersArray) {
if (!number.trim().isEmpty()) {int numberInt = Integer.parseInt(number.trim());
if (numberInt < 0) {
negativeNumbers.add(numberInt);
}
returnValue += numberInt;
}
}
if (negativeNumbers.size() > 0) {
throw new RuntimeException("Negatives not allowed: " + negativeNumbers.toString());
}
return returnValue;
}

Refactor State

This is a state to beautify your code. You’ve made a working code earlier, give a little touch to make it looks better. Even when you rewrite it, your code should work the same as before you did that. It will help you to write more efficient code.

Why TDD?

  • When you write a test based on a requirement, you’ll know that you would not miss any user requirement
  • TDD makes your code tidier, as we’ve talked about before, we know that TDD includes 3 states. If you follow those 3 states, your final code will look tidy because you did the refactor state
  • By using TDD, it will help you reduce the bug. This happens because, you will make your code after making your test, which means you should make a code that passed the test

Those are some reasons why TDD is useful for developers. Sure thing you can explore more advantages of TDD. For myself personally, using TDD is highly recommended. It is because TDD makes me easier to catch bugs than when I am not using the TDD. This could happen because I’ve written all the test needed before I code.

You’ve reached the end of the article, thank you for staying with me until the end. Keep learning and happy exploring!

Source:

--

--