A set of instructions to achieve an objective is a simple definition for an algorithm. In the computing space can be compared to a cooking recipe to solve a real world, computational or mathematical problem. This is defined as:
“Informally, an algorithm is any well-deﬁned computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.” (Cormen, 2009).
PROPERTIES OF AN ALGORITHM
The basic properties of an algorithm are: The language it is written (usually English); each step very self explanatory; Have at least one input; have at least one output and it should have a limited number of steps. (CareerRide, 2016)
EXAMPLE OF AN ALGORITHM
Example of an algorithm:
Search a string of at least five numbers (for example, 37540);
Identify all of the substrings that form numbers that are divisible by 3;
For example, applying the algorithm on the string 37540 should produce the following substrings (not necessarily in this order): 0; 3; 75; 54; 375; 540.
Solution in pseudo-code written using the English Language:
PSEUDO CODE Algorithm1 INITIALIZE numbersString WITH "37540" TYPE STRING INITIALIZE cursor WITH 0 TYPE INT INITIALIZE cursor2 WITH 0 TYPE INT INITIALIZE index WITH NULL type INTEGER FUNCTION START WHILE CURSOR IS LESSER OR EQUAL TO LENGTH OF numbersString SET CURSOR2 VALUE TO CURSOR1 + 1 WHILE CURSOR2 IS LESSER OR EQUAL TO LENGTH OF numbersString set INDEX VALUE TO SUBSTRING NUMBER -> (CURSOR, CURSOR2) IF INDEX % 3 = 0 PRINT INDEX END IF SET CURSOR2 TO CURSOR2 + 1 END WHILE SET CURSOR TO CURSOR + 1 END WHILE END FUNCTION END PSEUDO CODE
HOW ALGORITHMS BECOME COMPUTER PROGRAMS
As introduced above, algorithms brings a set of instructions to solve a problem. When this set is programmed in a programming language, compiled and set to run this become an executable computer program which may be executed and used as many times as wanted. Algorithms in action are computer programs running and using really powerful machines to solve thousands of inputs of data and process in a world wide scale. This is what computer programs do.
TESTING COMPUTER PROGRAMS
Computer programs can be tested using Unit Testing Code, which is basically a program to test another program using a predetermined input.
A unit test examines the behavior of a distinct unit of work. Within a Java application, the “distinct unit of work” is often (but not always) a single method. By contrast, integration and acceptance tests examine how various components interact. A unit of work is a task that is not directly dependent on the completion of any other task. (Massol, 2004).
Let´s introduce a calculator code:
Figure 1 Code of a Calculator Program
Basically as shown above, the add method receives two arguments, number1 and number2 and returns the result of the add operation of the two values. How could be proven that this piece of software work well? Let´s suppose we have a mathematical software using this method with thousands of operations depending on this small piece of code for all the ADD operations. Think now about this small piece of software failing!
So, the question comes out: How could this small piece of software be tested and proven that this works fine in our computer software?
Figure 2 Example of a Unit Test using the jUnit framework
The answer is simply: Writing the unit test for this software component. The code above simply calls the calculator passing two numerical values and expects the result. This is the way a software component can be effectively tested and proved with satisfactory results that the component is behaving normally.
In the example above, 10 and 50 is passed, and it expects the result of 60. It 60 is the output, the first requirement for the test is satisfactory and the unit test passes.
BENEFITS OF UNIT TESTING
Unit Testing improves a lot the final product quality. So the first benefit is the code quality achieved on projects that uses correctly Unit Testing.
The second benefit is the product integrity to be kept. When a Unit Test is coded the right way, all the requirements on how the piece of code (classes) needs to behave correctly are given. If someone else change any line of code and make a mistake, the Unit Test will not pass and you will easily detect a defect that is possibly coming to your software product.
The third benefit is the ease of understanding your software component by the test. If a developer doesn’t know anything about the product and wants to change anything, the unit test sometimes can be used as a documentation for the software, because it can be possible to understand the tiny parts of any component/class.
Cormen, T.H, 2009. Introduction to Algorithms. 3rd ed. Massachusetts: The MIT Press.
CarrerRide. 2016. Data structure – Algorithm, properties of an algorithm, types of algorithms. [ONLINE] Available at: http://www.careerride.com/Data-structure-algorithm-and-its-types.aspx. [Accessed 24 January 16].
Massol, V., 2004. JUnit in Action. 1st ed. Greenwich: Manning Publications.