acm-header
Sign In

Communications of the ACM

Communications of the ACM

Technical Opinion: Hello, World Considered Harmful


Why do people have trouble learning to program using the object-oriented paradigm? On the surface, it seems so obvious. This approach corresponds with the way we view things in the world—as objects with properties and behaviors. It should be more natural to learn OO programming than procedural. I suggest the way OO programming is initially presented is a significant part of the problem.

I recently taught an introductory Java class. No matter how often I explained the OO concept, some students continued to have difficulties in implementing it in their projects. One student went through the whole quarter writing all the code for his projects in a single class, even though the assignments, lectures, and project feedback clearly indicated that each project had to be implemented with two classes.

I reflected on my personal experiences with OO programming. I started writing code in the mid-1960s, so my past experience was heavily weighted toward procedural languages. Three decades later, I started studying books on Java. As I read them, I had a vague feeling that something was missing. Although the books extolled the virtues of the OO paradigm, the code samples and text were not really communicating the concept. Then I realized most of these books were written by people whose previous backgrounds were in procedural languages. I was not learning "object-think," because the authors were not writing from that perspective. The books didn't emphasize through example the idea of decomposing problems into individual objects.

As I worked more with Java and some quasi-OO languages, the concept became clearer. It began to feel more natural to create objects to use in my own code, rather than just using objects supplied with languages.

So what is the problem here? In a moment of inspiration I realized that it starts at the beginning. The "Hello, World" program has been a staple of introductory computer programming classes ever since Kernighan and Ritchie's C book [3] was initially published in 1978. With apologies to Edsger Dijkstra [2], I argue that "Hello, World" or an equivalent program in many Java books is harmful to the development of object-think.

For example, on the first page of The Java Programming Language [1] the following code (identified in the Index as "HelloWorld class example") is presented:

  • class HelloWorld {
  • public static void main (string[] args) {
  • System.out.println("hello, world");
  • }
  • }
  • Although this code runs, it communicates virtually nothing about the concept of user-created objects. Other than the initial occurrence of the key word "class," and that funny little dot in the print statement, it could just as well have been written 25 years ago in some procedural language. It doesn't instantiate any objects and use any object behaviors.

    Once this problem is identified, the solution is simple. The "Hello, World" needs to be rewritten to include a user-created object. All it takes is a few small but significant changes from the traditional program, as indicated:

    1. Make two copies of the original code.
    2. Change the name of the method in the first copy from main to something else (say, to printHello) and remove its arguments.
    3. Change the name of the class in the second copy (to UseHello).
    4. In the second copy, instantiate an object of the "Hello, World" class defined in the first copy.
    5. Use the method (printHello) from this instance to print the message.

    The result is shown in the following OO "Hello, World" Java code:

    • class HelloWorld {
  • public static void printHello() {
  • System.out.println ("hello, world");
  • }
  • }
    • class UseHello {
  • public static void main (String[] args) {
  • HelloWorld myHello = new HelloWorld();
  • myHello.printHello();
  • }
  • }
  • The important thing is to present this example to learners at the very beginning and have them type it into their own computers right away. This will help imprint the concept immediately, so they do not have to "unlearn" a procedural approach. For those who have already learned a procedural language, this approach emphasizes the differences from the beginning rather than reinforcing existing misconceptions through an example such as in traditional "Hello, World" Java code.

    Back to Top

    References

    1. Arnold, K. and Gosling, J. The Java Programming Language, 2nd ed. Addison-Wesley, Reading, MA., 1997.

    2. Dijkstra, E.W.G. Go To statement considered harmful. Commun. ACM 11, 3 (Mar. 1968).

    3. Kernighan, B.W. and Ritchie, D.M. The C Programming Language. Prentice-Hall, Englewood Cliffs, NJ, 1978.

    Back to Top

    Author

    Ralph Westfall ([email protected]) is an associate professor at California State Polytechnic University, Pomona.


    ©2001 ACM  0002-0782/01/1000  $5.00

    Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee.

    The Digital Library is published by the Association for Computing Machinery. Copyright © 2001 ACM, Inc.


     

    No entries found

    Sign In for Full Access
    » Forgot Password? » Create an ACM Web Account
    Article Contents: