TDD

How Test-Driven-Development works (Part 3)

This is part 3 of the blog series about how TDD (Test-Driven-Development) works. To start from the beginning you can refer to the start of the blog series here. We will continue our journey of TDD with the upcoming requirements to evolve our code along with the new tests. Requirement 4: Allow the Add method to handle an unknown amount of numbers [Java Test] Now we will refactor our code to implement the functionality of handling the sum of an unknown amount of numbers [Java Implementation] What we did is remove the existing functionality of handling the exception of more than two numbers. So after cleaning our code, our method will look as follows. Now when we rerun our tests all tests will pass as our requirement has been changed from handling only two numbers to handling an unknown amount of numbers. Requirement 5: Allow the Add method to handle newlines between numbers (instead of commas). [Java Test] [Java Implementation] To fulfill this requirement we just need to extend the split regex by adding |\n. Requirement 6: Support different delimiters In this requirement, it is specified that a user can use a different delimiter other than a comma which should also be accommodated by our String calculator. To specify the desired delimiter, it has to be at the beginning of the string that should look like this: “//[delimiter]\n[numbers…]” for example “//;\n4;6” should take 4 and 6 as parameters and return 10 where the default delimiter is “;”. [Java Test] [Java Implementation] For this requirement implementation, we did quite a lot of refactoring. We split the code into 2 methods. The initial method parses the input looking for the delimiter and later on calls the new private one that does the actual sum. Since we already have tests that cover all existing…

Continue Reading

TDD

How TDD (Test-Driven-Development) works (Part 2)

This is part 2 of the blog series about how TDD (Test-Driven-Development) works. You can refer to the start of the blog series here. We will continue with the next requirement to evolve our code for String Calculator. First, we will make the test fail for our second requirement and then refactor the Java implementation to make the test pass Requirement 2: When an empty string is passed as an argument to add() it should return 0 [Java Tests] [Java Implementation] Explanation: In the refactored code we have changed the return type of the add method from void to int and added the return statement to fulfill the requirement; if an empty string is passed then the method should return 0. To fail the last test you can return any other integer instead of 0. Requirement 2 Test Failed [Refactored Code] Requirement 3: Method will return the sum of numbers [Java Tests] [Java Implementation] References: The example of the string calculator has been taken from the Roy Osherove Katas. Don’t click the link until you’re finished with other parts of this blog series. This exercise is best done when not all requirements are known in advance.

TDD Test-Driven-Development

How TDD (Test-Driven-Development) works (Part 1)

Test-driven development (TDD) is a software development process in which a developer writes the code repeatedly in a very short development cycle: first, the developer writes an (initially failing) automated unit test that defines how a new feature should work or what to expect when extending existing functionality, then writes the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards. The following is the flow chart of the TDD cycle There are a lot of articles produced on TDD and Wikipedia is generally a decent beginning. This blog post covers real tests and the development of a simple string calculator. We will learn this test-first development approach by implementing the requirements one by one without knowing them in advance. In the following section, you will find the test script with respect to each requirement and afterward the actually produced code. Read, only one requirement at a time, write the tests and the implementation yourself and compare it with the results from this blog. Keep in mind that there are multiple different approaches to writing tests and implementation. This example is only one out of many possible solutions. Let’s begin our journey! List of Requirements Create a simple String calculator with a method int Add(string numbers) The method can take 0, 1, or 2 numbers, and will return their sum (for an empty string it will return 0) for example <“ “> or <“1”> or <“1,2”> Allow the Add method to handle an unknown amount of numbers Allow the Add method to handle newlines between numbers (instead of commas). The following input is ok: “1\n2,3” (will equal 6) Support different delimiters To change a delimiter, the beginning of the string will contain a separate line that looks like this: “//[delimiter]\n[numbers…]” for example “//;\n1;2” should…

Continue Reading

JavaScript array methods

JavaScript Array Methods Map, Reduce, Filter

Let us now try to see how JavaScript array methods Map, Reduce, and Filter work. The code snippets are shown below: To practice this tutorial, you need to have Visual Studio Code and NodeJS installed on your computer We will start by seeing the basic functionalities of the JavaScript array and then we will further use map, reduce, and filter methods to perform some actions on the array. The first step we need to take is to define an array with name ‘marks’ In this step, we defined an array with identifier marks with a size of six elements and then we populated the array with some values separated by commas. In the second step, we will perform some basic array functionalities like push, pop, and unshift and we will also see how we can traverse the array with for loop. There is an array method that helps you to validate if an element is present in it. So these were some basic array functionalities that we discussed. Now I will show you examples of performing actions on array elements with and without the JavaScript reduce method. First, we will see if we need to sum the array elements without reduce method see the following code snippet let totalMarks =0 for (let i=0; i<marks.length;i++){        totalMarks = totalMarks + marks[i]    } console.log(marks) // this will display the whole array in console console.log(“Sum of marks array =” + totalMarks) Second I will show you how we can simplify the above code snippet with the help of the JavaScript reduce method, see the following code snippet The following section explains how the JavaScript reduce method works The first argument is the callback function: We used only the first two arguments in our callback anonymous function i.e. <previousValue: sum>, <currentValue:…

Continue Reading