Previous Table of Contents Next


9.4. Hello, World

When discovering any approach to software construction, however ambitious its goals, it is reassuring to see first a small example of the big picture—a complete program to print the famous “Hello, World” string. Here is how to perform this fascinating task in the Eiffel notation.

You write a class HELLO with a single procedure, say make, also serving as creation procedure. Here is a minimal version of the class:

   class HELLO creation make feature
         make is
             do io.put_string (“Hello, World%N”) end
   end

In practice, however, the recommended Eiffel style rules suggest a better documented version:

   indexing
       description: “Root class for one-class system printing a simple
                    message”
   class HELLO creation
        make
   feature
         make is
               -- Print a simple message.
             do
               io.put_string (“Hello, World”)
               io.new_line
             end
   end -- class HELLO

The two versions perform identically; the following comments cover the more complete second one.

Note the typesetting conventions: Language keywords such as class appear in boldface monotype; identifiers such as the class name HELLO appear in italic monotype in this text, although the standard is italics; and comments appear in monotype, except for occurrences of identifiers in them.

The indexing clause does not affect execution semantics but serves to associate documentation with the class so that browsers and other indexing and retrieval tools can help users in search of reusable components satisfying certain properties. Here there is a single indexing entry, description.

The name of the class is HELLO. Any class may contain features; HELLO has just one, called make. The creation clause indicates that make is a creation procedure, that is, an operation to be executed at class instantiation time. The class could have any number of creation procedures.

The definition of that creation procedure, make, appears in the feature clause. Again, there can be any number of such clauses (to separate features into logical categories), and each one of them can contain any number of feature declarations. Here the code has only one.

The line starting with -- is a comment; more precisely, it is a header comment, which style rules invite software developers to write for every such feature, just after the is. (As will be seen in section 9.8.5, environment tools know about this convention and use it to include the header comment in the automatically generated class documentation.)

The body of the feature is introduced by the do keyword and terminated by end. It consists of two output instructions. They both use io, a generally available reference to an object that provides access to standard input and output mechanisms. The notation io.f, for some feature f of the corresponding library class (STD_FILES), means “apply f to io.” Here I use two such features:

  put_string outputs a string, passed as an argument, here “Hello, World”.
  new_line terminates the line.

Rather than use a call to new_line, the first version simply integrates a newline character, denoted as %N, at the end of the string. Either technique is acceptable.

To execute the software and print Hello, World, you need to construct a small system (the term preferred to program in Eiffel to emphasize the idea of building software by assembly of reusable components) and designate class HELLO as the system’s root class, with make specified as the system’s root procedure. Eiffel environments provide simple ways to construct an Ace file that specifies the root class, the root creation procedure, and other compilation options. In ISE Eiffel, for example, an interactive tool lets you enter the names of these two elements and builds the Ace file for you with all compilation options initialized to the most common defaults. You then click the Melt (quick compile) button to compile the system; after a few seconds, you are ready to click the Run button, which causes execution of the system. This outputs Hello World on the appropriate medium: a console on Windows or OS/2 or the starting window on UNIX or VMS.

9.5. The Static Picture: System Organization

We now look at the overall organization of Eiffel software.

9.5.1. Systems

An Eiffel system is a collection of classes, one of which is designated as the root class. One of the features of the root class, which must be one of its creation procedures, is designated as the root procedure.

To execute such a system is to create an instance of the root class (an object created according to the class description) and to execute the root procedure. In anything more significant than “Hello World” systems, this creates new objects and applies features to them, in turn triggering further creations and feature calls.

For the system to make sense, it must contain all the classes on which the root depends directly or indirectly. A class B depends on a class A if it is either a client of A, that is, it uses objects of type A, or an heir of A, that is, it extends or specializes A. (These two relations, client and inheritance, are described in more detail later in the chapter.)

The rest of section 9.5 describes the nuts and bolts of an Eiffel system and of its execution.


Previous Table of Contents Next