A Very Quick Comparison of Popular Languages for Teaching Computer Programming
Java and C are the most commonly used languages in the Computer 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
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
- 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
#includeint 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)
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.
- 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
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.
No comments:
Post a Comment