Today I wanted to do something different. 100 Days of code is not about solving competitive program alone. Competitive programming is important but to grow as a developer there are more than one aspects to it.

CP increases your logical thinking and it is important. Just as that the technical proficiency is also important. Today I took the liberty to make myself acquainted to Test Driven Development.

Before that we want to understand unit test and TDD. The unit test are written to make sure that individual units are working as expected. The smallest part of individual components like functions, procedures, classes, interfaces, etc.

Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases. This is opposed to software being developed first and test cases created later.

So basically unit test case is a piece of code you write to test your function or class and TDD states that you should write the test case before writing the function.

For today’s part I have done the 12 part kata listed by Osherove on https://osherove.com/tdd-kata-1

Before going through check it out yourself and try it.

Understanding:

  1. Only happy paths are tested.
  2. We have to move incrementally only one step at a time.
  3. Try to refactor your method then and there.

Case 1:

Create a simple String calculator with a method signature:

Try to start from the bottom, taking it up we see that zero should be returned when empty string is passed. Writing the test cases for that and completing the implementation we get

Case 2:

The next case would be to parse the independent numbers 1 or 2

Case 3:

The next case is where we have to handle the continuous numbers separated by comma.

Case 4:

Then moving on you can update the method to receive infinite numbers by expanding it to a loop.

I don’t want this blog to be a long one, I think these cases are enough to know about TDD.

You can find the Complete implementation of the kata here:

When done properly the benefits of TDD are enormous. At one go we are completing the testing along with development and also improving code quality.

#100DaysOfCode #Day4 #TDD #Junit

Lives to Learn