# Simplicity is the ultimate sophistication Leonardo da Vinci

 Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. [Brian W. Kernighan]
 There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies. [C.A.R. Hoare]
 Code is read much more than it is written. [software proverb]
Consider the following example from The Elements of Programming Style.

The code was originally written in FORTRAN. In that language, array indices start at 1. The implementation has been modified to reflect C++'s 0-based indexing. What is the purpose of this fragment of code?

```int matrix[4][4];

for (int i=1; i <= 4; ++i)
for (int j=1; j <= 4; ++j)
matrix[i-1][j-1] = (i/j) * (j/i);
```
Integer division is being used here. That means any remainder is truncated. As a result, whenever "j" is greater than "i", or "i" is greater than "j", the result is zero. The only situation where the result is not zero is when "i" equals "j". The purpose of this fragment is to initialize the 2-dimensional array as an identity matrix (all elements on the diagonal are "1" and all other elements are "0").

This code is exceedingly clever, but is it too clever for its own good? Code is read many more times than it is written. Any maintenance programmer facing this code is very likely to believe that something dark and sinister is going on here, and will superstitiously label the code "hands off". Another issue might be performance - every assignment requires two divisions and a multiplication.

Can we refactor this implementation so that it is more reader-friendly? The following implementation is entirely self-documenting.

```for (int i=0; i < 4; ++i)
for (int j=0; j < 4; ++j)
if (i == j)
block[i][j] = 1;
else
block[i][j] = 0;
```
But – every iteration of the inner loop requires an `if-else` evaluation. Is there an alternative with less overhead? Lets try replacing the `if-else` block with a ternary operator expression.
```for (int i=0; i < 4; ++i)
for (int j=0; j < 4; ++j)
block[i][j] = (i == j) ? 1 : 0;
```
The overhead seems to have been reduced – but has it really? The ternary operator is more compact for the human reader, but, as far as the compiler is concerned, it is still an `if-else` construct. Let's try one more idea –
```for (int i=0; i < 4; ++i) {
for (int j=0; j < 4; ++j)
block[i][j] = 0;
block[i][i] = 1;
}
```
All elements are assigned a value of zero, and then a single element in each row is re-assigned a value of one. While additional assignments are required, the utter simplicity is preferable.