Comments on "A Very Quick Comparison of Popular Languages for Teaching Computer Programming"

I wrote a brief article comparing C, Java and Python as a first language for Computer Science students new to programming. A number of people wrote with comments, which I've added below, along with my responses.

C++

On 25th Feb 2005, Terje Slettebo wrote:
You might also want to consider C++ as a language for teaching programming (and I think it's a plausible alternative, enabling both high-level and low-level programming). Here's the corresponding program in C++:
#include <iostream>

int main()
{
    int a,b;
    std::cin >> a >> b;
    int c=a+b;
    std::cout << c;
}

%> gcc add.cpp -lstdc++ -o add
%> ./add
Things to explain:

  • #include
  • functions and return values (and the exception that main() doesn't need to explicitly return a value, or you may add "return 0;" at the end))
  • variables and types
  • streams, the stream operators ("<<" and ">>") and cin/cout, and that it skips any whitespace
  • expressions (c=a+b)
  • compiling
Oh, and time to write, test and debug: The same as your Python example: About one minute (and it has about the same number of lines, as well).

It's rather interesting that Java - a language that purports to be _simpler_ than C++, actually makes it harder to write such a simple program as this (and other ones, as well).

Just one nit from your Python list, though: "requires less lines of code for any given problem, and is more readable - thus greater productivity." For "any given problem"? And less than what? Surely, you can't mean that Python programs will always be shorter for any problem, than the corresponding program in any other languages, even DSLs?

I have a bias against C++, which I have to admit may spring from ignorance or misunderstanding, but I made a pretty serious attempt to use it on a major project in the early 90s and ended up concluding plain C was easier and less clumsy. This is in contrast to Python, where I knew within an hour or so that I'd never seen a language so well designed and easy to use.

It's a fair point about my overly broad statement though - perhaps I should have said "requires less lines of code (and is more readable) than any more popular general purpose language, for almost any problem."

Smalltalk

On 8th April, 2005, Doug Clapp wrote:
Well, there's smalltalk, where you would have something like:

x := FillInTheBlank request: 'Enter a number for X'.   
y := FillInTheBlank request: 'Enter a number for Y'.   
Transcript show : x + y.
At first I was revolted by that, but I can see how it has a very verbose cleanliness.... My favourite would actually be APL, which has a box shaped character that means "get input". The program reduces to three characters: box+box

Lisp

On 20th April 2005, Erling Ellison suggested the following Lisp:
(print (+ (read) (read)))
although that appears to skirt some important issues, like giving the program a name so you can run it. I've been worried for some time that 'smug Lisp weenies' like Paul Graham know something I don't (well, ok, that's highly likely) but my studies of Lisp so far have not wildly impressed me. The Lisp community seems horribly fragmented, and the language itself (well, I looked at Common Lisp) has an "everything including the kitchen sink" feel too it. Further, if, as Paul Graham points out, you only need to run it on your own server, it's probably an ok choice, but if you want to distribute code to users, it all looks too hard.

Nevertheless, I can't help thinking, if Eric Raymond and Richard Stallman think Lisp is an important language to learn, that it possibly is...

Ruby

On 7th April 2005, Mark Hubbart wrote recommending Ruby, and shortly after David Morton wrote an almost identical email. Their arguments, jumbled together:
The first program looks like this:
a = gets.to_i
b = gets.to_i
c = a + b
puts c
Time to write, test and debug: less than a minute.

Concepts needed to learn:

  • variables
  • IO commands: gets, puts
  • difference between integer and string (what happens if the "to_i" is left off?)

pros and cons:

  • many similar pros as python.
  • More forgiving on whitespace.
  • Extremely object oriented language, better than java, but fairly easy to explain.
  • Complexity can run from really simple, like above, or moderate, like
  • classes and OO, to vary complex, with lamda and self modifying code (aspect programming?)

  • All Python advantages mentioned apply to Ruby, except perhaps the block structure feature, which is debatable. (Ruby's begin...end format makes it easier to notice improper nesting: a missing "end" is easier to notice than a missing brace) Also, it could be said that Ruby tends to be more terse. Compare Python's "sys.stdin.readline()" to Ruby's "gets".

Ruby - the cleaner Python? So many people have recommended Ruby to me in the last few months that I feel I have to try it some time soon. I'm not convinced that begin...end is an improvement on the meaningful white space I've come to love, but, in the same way it's dumb to skip Python because of meaningful whitespace, it'd be dumb to skip Ruby because it lacked it. The only comment I can really make is that Ruby doesn't seem to have quite the library support that Python does, nor quite the job prospects. But these appear to both be rapidly changing. Some good Ruby docs are: Chris Pine's "Learn to Program: A Tutorial for the Future Programmer" and the "pickaxe"

Perl

On 28th April 2005, Steven Cayford wrote:
I liked your article and the comments. I thought I would throw perl in as well.

First concept, took about a minute mainly since I forgot the operator precedence between string concatenation "." and addition "+" while pushing them both into the print function.

#!/usr/bin/perl
print "enter a: ";
$a = <STDIN>;
print "enter b: ";
$b = <STDIN>;
print $a + $b . "\n";
Second concept without the niceties:

#!/usr/bin/perl
print <STDIN> + <STDIN>;
Things to explain:

  • #!
  • $variables
  • the concept
  • auto-conversion from string to number
  • interpolation and concatenation of strings
  • precedence of operators and functions
I know perl has a reputation for being messy which might be considered a detriment for a beginning class, but it's easy to start learning, it's got a lot of depth, the variety keeps it fun, and there's an enormous amount of code out there.

Otherwise, probably similar pros and cons as python and ruby.

As an ex-Perl-fan I can only say that if I hadn't learned Python, this whole article would have been about why Perl is a good language for teaching. But Python is Perl without the line-noise. It's Perl cleaned up for the 21st century. It's 95% of the power with triple the clarity. I loved Perl, but Python's simply better. Sorry.

Shell

On 31st May 2005, Nathan Laredo wrote:
I think it's also important to teach good shell programming -- your python example opens the door to all these (examples assume they were typed at a shell prompt):

here's the whole thing for bash/sh/ksh/zsh:

read a; read b; echo $[a + b]

here's the whole thing for awk:

awk 'BEGIN { getline a; getline b; print a + b; exit; }'

or alternatively:

awk '{ a = $0; getline b; print a + b; exit; }'

and here's the whole thing in perl (though I dislike perl more than C++):

perl -e 'print <> + <> . "\n"'

Of course all of the above can be formatted as needed when in a script file rather than entered at the shell prompt. Some people write simple programs in languages like java and C++ when a trivial shell script would have gotten the job done in less time.

The right tool for the job is an important lesson to learn, and competent shell scripting is a lost art. If you're wasting time at work coming up with java code to read and then add two numbers, your employer might not be so happy, but of course shell scripting is clearly the wrong way to do visualizations of satellite orbits.

Then again, I may be the last person in the world who looks for excuses to use awk...

I have to say I'm sceptical.... There's definitely a place for shell scripting - bootscripts for example - but anything that even smells a little bit like an application would be better done in Python in my opinion. Perhaps I'm biased, but shell scripting to me has always felt like smacking my head against a brick wall in a totally different but no less unpleasant way than programming in C++.

Serious working Unix programmers would probably be well served by really understanding shell and awk, but I think, although they solve the above problem easier than Java, they are generally so baroque as to be a very poor choice for beginners.

I agree though that it's very important to impress upon students the idea that "if all you have is a hammer, every problem looks like a nail" - and that they should aim to constantly broaden their repertoire of tools. I also think a class devoted to writing one-liners mightn't be a bad idea - it requires more mental discipline than simply churning out the first bloated solution that comes to your head. But again - these are not issues beginners should be grappling with.

Patrick Jordan - patrick@ariel.com.au - 2005-05-31


...original article