A Very Quick Comparison of Popular Languages for Teaching Computer Programming

This article has been translated into Serbo-Croatian here: http://science.webhostinggeeks.com/brzo-uporedivanje. Thanks to Jovana Milutinovich for creating this translation.

In the CS department where I currently teach I recently got involved in a debate on which programming language should be used to teach beginners. Java and C are the most commonly used languages in the department, and for many subjects this is appropriate, but not (I believe) for absolute beginners. I believe Python is a much better choice for beginners, and to firm up my own position I performed the very brief, very unscientific test described below.

***

The Test

I wanted to look at what was involved in writing very simple programs in a (small) variety of languages. The languages I chose were BASIC, C, Java and Python. I used C and Java because these are in common use in the department (and in other teaching institutions. I chose Python because I love it, and think it an excellent choice for teaching, and I chose BASIC because, well, it was just too easy.....

"Hello World" seemed a bit too trivial, so I decided on the relatively simple task of reading two numbers from the user, adding them together and printing out the result. My interest was

  • How long did it take to write and debug the code
  • How many things does a student need to understand in order to write this code
The times given to write the code are obviously not meant to be representative of the time required by a student, but I believe that they give a roughly accurate measure of comparison. I am reasonably skilled (1-5 years professional experience) in each language, so I don't think I was unreasonably biased.


BASIC

I learned to program, back in the late 70s, on a Level I TRS-80, and on a time sharing system that my high school had occasional access to. The program is trivial in good 'ol BASIC:

10 INPUT A
20 INPUT B
30 C=A+B
40 PRINT C

RUN

Time to write:

15 seconds. I admit I don't have a BASIC interpreter handy and did not test this, but I just know it works. (OK, I fired up the TRS-80 emulator and actually ran it - it works...)

Things to explain:

  • Line numbers
  • Variables
  • INPUT
  • PRINT
  • RUN

Pros and Cons

BASIC is very easy for beginners to get started with, but it is an old, poorly designed language, lacking in almost every modern feature. Visual BASIC adds a lot to "good 'ol BASIC", but it is not appropriate (I believe) to teach a single-platform proprietary language. And it's still not really a good language....


C

#include <stdio.h>

int main(int argc, char*argv[]) 
{
    int a,b,c;

    scanf("%d",&a);
    scanf("%d",&b);

    c = a+b;
    printf("%d\n",c);
}

%> gcc -o add add.c
%> ./add

Time to write:

about three minutes, including debugging.

Things to explain:

  • #include, functions (main), return types, argc, argv
  • variables, types (int)
  • scanf (and pretty soon it's limitations and how to work around them)
  • printf, format strings
  • pointers (already!!)
  • compiling, braces and semicolons

Pros and Cons

C was designed by top hackers for their own use. It was designed for writing operating systems, compilers and other system tools, and in this role it has become almost totally dominant.

It can provide excellent performance (assuming good choice of algorithm and good C skills) and allows low level hardware access, but these are not normally things required by the beginner. C's use of pointers are a source of frustration and confusion for beginners, but they are essential in even fairly trivial programs (like the one above, albeit in a trivial way).

Further, C's string handling is weak compared to many other modern languages (the scanf function used above is notoriously problematic).

C is a major and very important language, and all programmers should have significant exposure to it. It is however a terrible language to teach beginners. There is too much C that has to be explained, leaving less time for explaining programming.


Java

import java.io.*;
public class Addup
{
    static public void main(String args[])  {
        InputStreamReader stdin = new InputStreamReader(System.in);
        BufferedReader console = new BufferedReader(stdin);
        int i1 = 0,i2 = 0;
        String s1,s2;
        try {
            s1 = console.readLine();
            i1 = Integer.parseInt(s1);
            s2 = console.readLine();
            i2 = Integer.parseInt(s2);
        }
        catch(IOException ioex) {
            System.out.println("Input error");
            System.exit(1);
        }
        catch(NumberFormatException nfex) {
            System.out.println("\"" + nfex.getMessage() + "\" is not numeric");
            System.exit(1);
        }
        System.out.println(i1 + " + " + i2 + " = " + (i1+i2));
        System.exit(0);
    }
}
%> javac Addup.java
%> java Addup

Time to write:

19 minutes! Actually, I spent about 15 minutes, failed, then searched Google for an example. The code above is copied from a web page, which, tellingly I thought, starts with the words "It might be thought that a programme that reads in two user entered integers and prints out their sum would be a simple piece of code".

Obviously, this code is not perfectly equivalent to the other programs presented here since it does proper error checking, however Java makes it difficult not to do error checking. You must catch the exceptions, and having caught them you might as well do something with them.

I'm actually kind of embarassed I had so much trouble with this - I've been working on a commercial Java package for two years, but because it's GUI based I rarely have to deal with reading from the console. Real Java programmers will probably look down on me with a mixture of pity and disgust. Such is life.

Things to explain

  • import, classes, semicolons braces
  • public, static, void, String, main args[]
  • InputStreamReader, BufferedReader, System.in
  • variables, types
  • try, catch, exceptions, readLine, parseInt
  • System.out.println, compiling, running

Pros and Cons

Java is a useful language for cross-platform GUI development, is a robust platform for OO development, and has an extensive and highly evolved set of class libraries. Perhaps most importantly, it's the most popular language around and there's lots of jobs for Java programmers.

The extensive class library is however quite daunting. It appears there's a class for almost everything, and much of "programming in Java" seems to consist of "searching for the right class". Even after two years I find I cannot do much in Java without constant reference to the documentation.

Java enforces Object Orientation, exception checking and strict typing - these are all (arguably) good things - they make it easier for a group of programmers to robustly create large systems. But for small problems (such as those faced in introductory programming classes) these things become nothing more than a complicated, time-sucking burden.

The employment reason alone is sufficient to make Java a "must teach" lanaguage, but I believe we do our students a disservice if this is the best language we show them.


Python

import sys

a = sys.stdin.readline()
b = sys.stdin.readline()
c = int(a) + int(b)
print c

%> python add.py

Time to write:

about one minute, including testing and debugging.

Things to explain

  • import
  • variables
  • sys.stdin
  • readline (reads a string)
  • int (converts a string to an integer)
  • print

Pros and Cons

Python has an awful lot of good points:

  • enforces good programming style (indentation is meaningful)
  • OO available but not enforced
  • Exceptions used but not enforced
  • is not a toy or academic language - much real world work is done in Python
  • allows concentration on algorithms and problem, not on language features and shortcoming.
  • is cross platform and has a powerful set of libraries
  • is safe - it has dynamic run time type checking and bounds checking on arrays
  • has powerful built-in data types - dictionaries, lists, sequences, functions, sets (in 2.4)
  • has powerful built-in control structures - simple looping over sequences, map, generators, list comprehensions, regular expressions...
  • requires less lines of code for any given problem, and is more readable - thus greater productivity.
For teaching as a first language however it has some specific advantages. As can be seen from the examples above (ignoring BASIC), Python requires less time, less lines of code, and less concepts to be taught to reach a given goal. This allows more time to be spent on the important things. Further, some common student errors are completely byassed in Python:

  • end of line is end of line (no forgotten semicolons)
  • no type declarations
  • true block structure always obvious (no missing braces error)
  • dynamic memory allocation and garbage collection
Finally programming in Python is fun! Fun and frequent success breed confidence and interest in the student, who is then better placed to continue learning to program.

But Python is Just a Scripting Language

Python is often dismissed as "just a scripting language" (Perl and Ruby also suffer from this silly bigotry). This is simply incorrect. It is not "just a scripting language" - it is a full featured very high level language that is ideal for many applications, including simple scripting duties.

The fact that you can write "quick and dirty" scripts in Python is an advantage, not a disadvantage, since scripting is actually an essential part of professional programming. If students don't know Python (or Perl, or Ruby, or....), they will waste a lot of time trying to solve script-like problems in Java.

But Python is Slooooooow

Python is an interpreted language, and this does add some overhead. Dynamic bounds checking, dynamic typing and other clever Python things slow it down even further. Python can be orders of magnitude slower than equivalent C code. However

  • Many, many applications are not compute bound. To use a high performance language for them exemplifies the sin of early optimisation.
  • Python interfaces well to C - enormous gains can be made by coding critical sections in C
  • Time saved coding in Python, and the much greater simplicity of the code written, allows much more time for experimentation in more efficient algorithms - often much more fruitful than simply running a bad algorithm very quickly.

Conclusion

C and Java are important languages - for the concepts they embody, for the employment prospects, and for the classes of problems they solve. Students must be given a thorough grounding in these languages. They do not however form a sufficient arsenal for the professional programmer - a good "scripting language" is a must - nor are they good languages to teach students new to programming. They have a lot of overhead and other impediments that take a lot of the pleasure out, and make both the student's and the teacher's jobs more difficult than they ought to be.

There are people who would argue that the impediments are part of the discipline of programming - students must learn to catch their exceptions, use pointers, declare all their types and so forth. Maybe, maybe not - but there's time for that later. Let's let students have the simple joy of small successes that we (well, "I" anyway) had when we were starting. Patrick Jordan - patrick@ariel.com.au - 2004-12-14


Postscript (Feb 2006)

Aside from the comments above, a vast number of people wrote to me after this article appeared on Daily Python to point out that there was a simpler way to do it in Python:
a = input()
b = input()
c = a + b
print c

%> python add.py
(various one liners like 'print input()+input()' were also suggested and work just as well but I'd argue are less useful for teaching purposes). Further, since input() accepts any valid Python expression, this program just works for a whole range of inputs - ints, floats, strings (it'll concatenate them - but note that they must be in quotes else they'll be interpreted as variable names) or expressions such as "3.14**2". Further proof, as if it was needed, of the beauty of Python.

Comments

I've received a number of comments on this topic, particularly on languages I didn't mention (Ruby, C++, Smalltalk, LISP)...
Read Comments
Read Even More Comments