Bojan Nikolic: Numerical and Quantitative Methods in C++

[website home] | [BN Algorithms]

Avoiding Repetition

In the first article of this series, I described a very simple C++ program to calculate the arithmetic mean of five numbers. The intention is to use this simple problem as a worked example on how to write good programs. The first area I will look at is avoiding repetition in your programs.

Here is a section of the program in the first article:

double sum =0;
for (size_t i =0 ; i < 5 ; ++i )
{
  sum += data[i];
}

std::cout<<"The mean is: "<< (sum / 5) <<std::endl;

As you can see, it specifies at two separate points that there are five elements in the array from which we want to compute the mean:

  • Once when looping over the elements of the array (i<5)
  • The second time when dividing the sum by the number of elements to compute the mean (sum/5).

Why is this a bad idea? If, for example, you change the number of elements in the array, you need to remember to change two numbers. This might not seem so bad, but, surprisingly, it is extremely error prone! There are a huge a number of bugs in working programs due to one change requiring correction of code in several places.

Improved program: mean_v2

Here is an improved version of the mean calculation program:

// mean_v2.cpp
// Bojan Nikolic <bojan@bnikolic.co.uk>
// Avoid repetetion

#include <iostream>

int main(void)
{
  const double data[] = { 1,2,3,4,5};
  const size_t n = 5;

  double sum =0;
  for (size_t i =0 ; i < n ; ++i )
  {
    sum += data[i];
  }

  std::cout<<"The mean is: "<< (sum / n) <<std::endl;
}

The change compared to version 1 of the program is that we declare and initialise the variable n to the number of elements in the data array, in this case 5:

const size_t n = 5;

Whenever we need to use the number of elements, we then use n instead of 5.

As described above this approach has the advantage that if we wish to compute of more or fewer numbers, we need to change only one, conspicuous, number.

But there is a second important advantage: this version is much more expressive about the intentions of the programmer, and that greatly increases the readability of the program. For example:

sum/ 5

reads as divide the sum with the number five. While:

sum / n

reads as divide the sum with the number of elements in the sum – this is exactly what the arithmetic mean is and so this version is much more expressive.

Summary

It is highly desirable to minimise the repetition of all ideas in a program, including repetition of constants, and expressions, algorithms, etc. In this example I showed how introducing a variable to remove repetition of literal constant improves both the maintainability and readability of the program.

The next article in this series looks at further improving readability by separating input, output and computation sections of the program.