“Hello, World Considered Harmful” Considered Harmful

Ralph Westfall’s “Hello, World Considered Harmful” (CACM, 2001) begins by making a sensible point: the classic K&R example program “Hello, world” is not a very useful example for an introductory object-oriented programming course in Java:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("hello, world");
    }
}

Fair enough: “Hello, world” is fundamentally procedural, and doesn’t teach students anything about how object-oriented programming (it might also teach them that Java is a bondage-and-discipline language that forces them to endure plenty of unnecessarily verbose syntax, but they are going to learn that eventually regardless).

Unfortunately, Westfall’s solution to the problem falls short of the mark:

class HelloWorld
{
    public static void printHello()
    {
        System.out.println("hello, world");
    }
}
public class UseHello
{
    public static void main(String[] args)
    {
        HelloWorld myHello = new HelloWorld();
        myHello.printHello();
    }
}

Now, we can raise a few minor objections: for example, it is pointless to create an instance of a class that contains only static methods, and it is bad style to invoke a static method on an instance, IMHO.

But the essential point is that Westfall’s solution merely applies some irrelevant syntactic sugar. The reason that “hello, world” isn’t a good example of object-oriented programming is because it isn’t object-oriented in the first place. The intuitive appeal of object-oriented programming stems from the fact that it is similar to objects in the “real world”, which combine data and behavior. An instance of the HelloWorld object has neither data nor behavior. In fact, the modified example serves mostly to showcase an annoying property of Java: the dependence on static methods as a way of emulating global functions.

So why not apply a little creativity, and use a fresh example that actually illustrates the intuitive appeal of objects? Model a few real-world objects as classes, and give them some behavior: for example, simple geometric shapes like Triangle and Circle, which can then eventually be developed into a lesson on inheritance.

(Another improvement would be to use a better teaching language than Java, but that’s another story.)

Advertisements

Leave a comment

Filed under Advogato, PostgreSQL

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