The cognitive load of C: arrays

In a similar guise to functions, arrays in C have a higher cognitive load, largely due to their reliance on pointers. Creating the most basic arrays are trivial:

int anArray[100];

This creates an integer array named anArray which has storage for 100 elements. So far so good. The first problem arises when it is mentioned that arrays start their indexes, used to access array elements at 0. Not 1, 0. It is hard for the novice programmer to understand why this is the case – and it honestly isn’t until much later that it all makes sense. To the novice, starting anything at 0 is non-sensical. It’s like saying “the office is on the 0th floor”, or “put your coat in the 0th closet in the hallway”. Not only that, now the programmer is accessing element 0 to 99, not 1 to 100. So this puts a higher onus on getting any code related to the array correct. Many novice programmers will go on to write a C loop like this to process the code:

for (i=1; i<=100; i=i+1)
    anArray[i] = i;

Of course, C is not designed to help the novice, so it will compile the code, and it will run. Problem is that it accesses element 100 of the array, which does not technically exist.

There is also the issue of memory allocation. Novice programmers might try to create a 2D array of the form:

double aMatrix[2000][2000];

And do something with it, like assigning each element a value. Seems fine right? Until they compile it – oh, nothing happens… it compiles fine. Then they find out it crashes. But why? Simply put, there is not enough storage on the stack to store the array. So the cognitive load of the user is increased because they have to obtain an understanding of both the types of memory used by C, and the dynamic arrays that must be used to create large arrays.

There are three more issues with arrays in C: slicing, printing, and strings. Both slicing and printing require the use of nested loops to achieve, although not onerous, they do produce more complex code, where it is easier to make a mistake. Take for instance the task of processing the matrix aMatrix and obtaining the mean of a 3×3 region centred on each element… also noting that this can only be preformed on the inner set of elements.

int i, j, x, y, n=2000, sum, mu;
for (i=1; i<n-1; i=i+1)
    for (j=1; j<n-1; j=j+1)
        sum = 0;
        for (x=-1; x<=1; x=x+1)
            for (y=-1; y<=1; y=y+1)
                sum = sum + aMatrix[i+x][j+y];
        mu = sum / 9.0;

If the novice gets just one of the indexes wrong, they will spend a long time trying to find the problem. Even an experienced programmer might get something wrong here. Consider now the equivalent code in Julia:

n = 2000
for i=2:n-1, j=2:n-1
    block = aMatrix[i-1:i+1,j-1:j+1]
    mu = mean(block)

This likely has a reduced cognitive load, and produces code that is easier for the novice to decipher.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s