The cognitive load of programming languages on the novice programmer

A programming language designed for use by novice programmers should follow the principles described in cognitive load theory (CLT), which helps reduce the load on working memory. The basic cognitive architecture is divided into three stores of memory: sensory memory, short-term memory, and long-term memory [1]. Information from the environment enters the mind through he sensory memory (decay period is maybe several hundred milliseconds). The short-term memory, also known as the “working memory”, performs conscious processing (decay period = 15-30 seconds). Long-term memory is the permanent store of information. It is the working memory that is effected by cognitive load: it is not stored for that long, and there is a very limited capacity (7±2 items). These limitations in working memory pose a severe constraint on learning  capacity.

The cognitive load on the novice programmer is always high. This is partially because of the large assortment of concepts to be learned, and the immense syntactic structure of some languages. Let’s take java for example. The quintessential program for beginners is “Hello World”. Here is the Java version of the program.

public class HelloWorld { 
   public static void main(String[] args) { 
      System.out.println("Hello, World!");
   }
}

Many times novice programmers are told that the key line is System.out.println(), and to ignore the  terms public, class, main, String[], and args – they will be explained later. It is however difficult to ignore these things in such a simple program. In five lines of text this  simple program introduces the ideas of visibility modifiers (public), OO (class), method names (main), parameters, and arrays. This provides the perfect scenario for information overload. Compare this against the same task in Python or Julia:

Python:     print "Hello, World!"
Julia :     println("Hello, World!")

The cognitive load here is quite low in comparison, as there are no requirements beyond using a statement to print the string “Hello, World!” to the screen.  Another case involves I/O in C. Consider the following code in C to obtain an integer from the user, and print it out:

#include <stdio.h>

int main(void)
{
    int n;
    scanf("%d", &n);
    printf("%d", n);
    return 0;
}

This simple program requires the novice programmer to understand the following:

  • C requires a library to be included for I/O.
  • C requires variables to be declared before they are used.
  • C requires the exact format of the data to be input (or output) to be specified by means of formatting codes.
  • C requires a basic understanding of the address-of operator (&) and hence pointers in order to successfully store a number input by a user.
  • C requires come understanding of void parameters, and return types.

This is a simple program,  albiet one with a potentially high cognitive load. This is further complicated by the fact that erroneous input by the user will break the program.  Consider a similar program in Python:

n = int(input("number? "))
print(n)

In this program the novice programmer is still required to have an understanding of the function input(), which takes any input typed by the user, and int() which converts the input to an integer, but there is no other requirement to understand how or where the memory is stored. This therefore has a lower cognitive load. Julia uses a similar construct:

n = chomp(readline(STDIN))
n = parse(Int, n)
println(n)

The only difference here is that the function readline() reads input from the user, chomp() removes the trailing RETURN key pressed by the user, and parse() turns what is essentially a string into an integer. As such it probably has a higher cognitive load than Python, largely because the used is not use to words like chomp and parse.

Most teachers forget the effect of cognitive load on trying to program because we are experts, whose programming knowledge is tacit, i.e. it can’t readily be taught in the same way that people recognize a face in a crowd. When a person sees a face they recognize, they are not conscious of the individual features of the face, only the face as a whole. Similarly an expert programmer may look at a program and understand what the program does, without having to go through every line of code.

Languages such as C are rife with syntax that increase the cognitive load of the novice programmer. Even if they aren’t taught the syntax, they will encounter it, and have to deal with it. Take for example the notion of the ternary operator, basically a shortcut decision operator found in many languages.

n = (x != 0) ? x+1 : 4;

This basically says that if x is not equal to 0, assign n the value x+1, otherwise assign n the value 4. The problem is that it requires more extensive knowledge of the language than novices require. It could have also been written as:

if (x != 0) 
    n = x + 1;
else
    n = 4;

This is not as compact, but much more readable to the novice programmer, and more importantly has a lower cognitive load.

[1]  Atkinson, R. C., Shiffrin, R. M., “The psychology of learning and motivation: advances in research and theory”,  in The psychology of learning and motivation: Advances in research and theory, Vol. 2, pp. 89–195 (1968)

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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