Beyond Hello World

A common pattern for introducing a programming language is the Hello World program.

But what exactly can you tell about a language from looking at its Hello World program?

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

What does that tell you about object-oriented programming using classes?

#include

int main(int argc, char *argv[])
{
    printf("Hello, World!\n");
    return 0;
}

What does that tell you about pointers?

module Main where

main = putStrLn "Hello, World!"

What does that tell you about laziness and functional purity?

:- write('Hello, World!'), nl.

What does that tell you about nondeterminism and backtracking?

<?php echo "Hello, World!"; ?>

What does that tell you about poorly-designed languages and libraries? (PHP isn’t all bad. I spent years using PHP for most projects of my own free will. And there is a lot of evidence that it’s easier than many languages for helping novices create interesting things. As with many simple technologies, the problems appear when you start applying them to more complex projects.)

The answer is that none of these examples provides much indication of how other (even simple) programs can be written in them; nor do they demonstrate the language’s characteristic features or common idioms. When I saw a lot of Hello World programs in this quiz, I realised that the only feature to distinguish many of them was the name of the standard library function used to print a string. The features which make a language special were going unmentioned.

Benefits of Hello World

Hello World has two positive features:

In a tutorial for a new programming language, it provides a simple test: if you type in and run this short program, and see the output “Hello, World!”, then you’ve at least managed to install the compiler or interpreter correctly.

It is also a kind of Rosetta Stone. If you know how to write Hello World in one language, and you see a Hello World in another language, you can use the common patterns to (experimentally) understand the new ones. Here’s one:

hello: db 'Hello World', 13, 10, '$'
mov dx, offset hello
mov ah, 9h
int 21h

But can you guess what mov and int mean in that language? (And what does this example tell you about the Intel 8086 instruction set?)

The reliability of this kind of approximate program translation is therefore quite limited.

But in regard to the first point, Hello World is still a useful — and short — piece of code for testing that a programming environment is ready for use.

Improving on Hello World

If Hello World is inadequate, what canonical program would better serve to demonstrate the salient features of a programming language?

  1. Must it use variables?
  2. Must it have flow control?
  3. Must it actually compute something?
  4. Must it use non-primitive data structures?
  5. Must it exhibit use of the language’s code organisation features?

These are some of the more generic properties that programs can have.

The first problem is deciding which language features are fundamental to programming. A Perl programmer might suggest extracting values from strings, but a Logo programmer might say the obvious example is drawing geometric figures on the screen. All languages have peculiar strengths. The aim is not to choose a problem that shows off the abilities of our favourite language, but to show how the language can be used for the same tasks as other languages.

There are also constraints of brevity and simplicity. All general purpose programming languages allow compilers or translators to be written; a self-hosted compiler is a common benchmark of a language’s practicality. But (excepting, perhaps, in the case of LISP), a compiler or interpreter example in every programming language article on Wikipedia is untenable. Moreover, compilers (despite their importance to computing) are very abstruse compared to most other programs. Requiring even a basic knowledge of them for learning a new language is prohibitive.

So the second problem is to find a compromise between simplicity and variety of language features employed.

Possible alternatives

Examples of simpler programs from introductory programming are programs such as the factorial function, Fibonacci sequence, and Euclid’s GDC algorithm. These often demonstrate the first three features from the list above, but do not often touch on the remaining two.

Part of the reason is that they are simple numerical functions. One of the benefits of an example program that exhibits the fourth property would be to show that a computer does not just manipulate numbers. It can manipulate any kind of information as long as the structure and operations can be specified. Programs that perform simple string operations (such as reversing a string or counting words), arguably demonstrate this property.

Strings are built in to most languages (with the notable exception of C) and so are arguably primitive, too. Demonstration of user-defined structures is also desirable; common examples of these are collection types such as queues.

It may be harder to demonstrate the last property convincingly in a short program. Features that aid code organisation are most useful in large and complicated programs. So I will consider that an optional property; small programs have less scope for organisational efficiency.

One suggestion (from David Pearce) was Conway’s Game of Life: a simple simulation of finite automata. This seems like a good demonstration of most of the properties. It also has the advantage of being accessible: you don’t have to understand much theory to appreciate that patterns arising out of simple rules. It does have the potential downside of being difficult to get right, especially the task of updating a simulation based on its current state; but that provides an opportunity to show how a language helps with that issue.

What other possible worthy successors to Hello World are there? Comment below if you can suggest some!

Advertisements
This entry was posted in Programming, Rants and tagged . Bookmark the permalink.

12 Responses to Beyond Hello World

  1. gdm9000 says:

    I’ve taken on this project myself, and have chosen a variant of FizzBuzz. My ground rules include trying to demonstrate function calls and comments, making the program be executable from a Linux command line, and trying to write idiomatically in each language. It is just enough to teach me a bit about each language. You can view my results so far at http://gdm9000.wordpress.com/. Also, some other sites you’ll be quite interested in include http://rosettacode.org/wiki/Rosetta_Code and http://99-bottles-of-beer.net/

  2. Al Sweigart says:

    “Guess the Number” should replace “Hello World”

    Hello! What is your name?
    Albert
    Well, Albert, I am thinking of a number between 1 and 20.
    Take a guess.
    10
    Your guess is too high.
    Take a guess.
    2
    Your guess is too low.
    Take a guess.
    4
    Good job, Albert! You guessed my number in 3 guesses!

    It’s short, it has integers/strings, it has a loop. it has if-else, it has string concatenation, it involves random numbers (which makes it non-deterministic) and feedback, and is a bit fun. In most programming languages it takes just a couple dozen lines of code.

    • ejrh says:

      I like this one a lot! Short and fun, and exhibits a lot more programmatic flavour than merely printing a constant string.

  3. For every new language I’m interested in, I write small ascii perlin noise renderers, serves as a benchmark too.

  4. The benefit of HelloWorld: it gives a boilerplate of the minimal side-effecting program. So, it is a programming “zero” to start writing your program from, not a didactic example to learn the language. Anything less, and we cannot run it, or cannot see if it works. Anything more, and it’s hard to tell what’s important and educational, and what is boilerplate. Consequently, we still need an example of a runnable program with output, it helps to ignore the boilerplate in the next problem-solving example, be it Life or whatever.

  5. ph says:

    Tutorials demonstrating the core language characteristics are valuable thing, but I don’t think helloworlds are ever really intended for that. I think they are for checking your compiler/interpreter setup works, and nothing beyond that.

  6. jtmacleod says:

    Hello World doesn’t need to be replaced with something more extensive. The purpose of Hello World is to show the minimum amount required to produce a functional program.

    It is a starting point. All of the other things that Hello World doesn’t do? Those things are what the rest of the book/website is for.

  7. Pingback: More blogging statistics | EJRH

  8. fileformat says:

    I have a website that tests regular expressions in multiple languages. Each language has a backend server that implements the same API. It is more complex, but also shows real-world code that does a couple of common things:
    – string concatenation
    – regular expressions
    – serving a webpage
    – html escaping
    – json encoding

    The code for each programming language is on github: https://github.com/fileformat

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