Sunday 20 December 2009

Appropriate complexity in Java and Python

I've blogged previously that the notion of appropriate complexity is important in determining how easy it will be to learn a programming language. Ideally, small, simple problems should be solvable with small, simple programs, and program size or complexity should not need to increase substantially if there is a small increase in the size or complexity of the problem - always assuming, of course, that we remain within the 'comfort zone' of the language.

Let's consider how well Java meets this requirement by looking at the traditional first program: one that prints "Hello, World!" on the console. If we were dogmatic about exploiting Java's object-oriented nature, then such a program would have to look something like this:
public class Greeting
{
  public void display()
  {
    System.out.println("Hello, World!");
  }

  public static void main(String[] args)
  {
    greeting = new Greeting();
    greeting.display();
  }
}
A complete novice who is already nervous about the challenge of learning programming will probably regard this code with dismay and wonder just how much more difficult writing a non-trivial program is likely to be.

One way of minimising the complexity is to develop and execute code within an environment that hides much of the 'scaffolding' - BlueJ, for example. Alternatively, we could simplify things by adopting a largely procedural style:
public class Greeting
{
  public static void main(String[] args)
  {
    System.out.println("Hello, World!");
  }
}
But even this simpler code is replete with potentially confusing details. The novice will no doubt be able to identify the single line of code that causes a message to be printed on the console, but will wonder
  • Why the line appears inside something called main
  • What purpose is served by String[] args
  • What the words public, static and void signify
  • What a class is, and why the code has to be enclosed within it
How does one deal with such questions? Is it better to explain every detail of the program, running the risk of confusing or intimidating the less experienced, less confident students? Or is it better to defer explanations until later, running the risk that students will not be confident about writing their first few programs because they don't understand everything that they are expected to type? Both approaches clearly have drawbacks.

Python suffers from no such problems, offering us what is surely the simplest version possible of the traditional first program:
print 'Hello, World!'
(The Python 3 version includes parentheses, of course, because print is a built-in function rather than a statement in Python 3.)

2 comments:

  1. A quibble: You say python's hello world is "surely the simplest version possible of the traditional first program". A simpler version is found in languages that allow you to do away with the quotes, such as bash:

    echo Hello, World!

    In some languages (php) you could even do:

    Hello World!

    and have that print itself out.

    ReplyDelete
  2. I guess so, cbr - although I would argue that omitting the quotes reduces the clarity. A pathological example would be:

    echo echo beach is a song by martha & the muffins

    ReplyDelete