Contents

4.2 The More Tool

The UNIX more command pauses after each screenful (22 lines), printing "--More--" at the bottom of the screen. If the user types a carriage return, one more line is displayed. If the user types a space, another screenful is displayed. If the user types an integer, that number of lines is printed. If the user hits d or CONTROL-D, 11 more lines are displayed. The UNIX version also has nine command line switches, but since I've never used any of them I didn't bother to implement them. (That's left as the proverbial exercise for the reader. See the UNIX documentation for a description of the nine unimplemented options.) The More program is shown in Listing 61.

When I tried to run More on the PC, I ran into a name clash. DOS already has a command called MORE. It differs from the UNIX MORE because DOS MORE has to be used as a filter. That is, you can say TYPE ANYFILE.EXT | MORE and it will show you a screen full at a time, but if you type MORE ANYFILE.EXT it just sits there waiting for you to enter data from the keyboard, which it will display one screen at a time. When that happened I thought the system crashed.

I could have left the name of my program Show, but that doesn't solve the problem. I had fallen into the habit of typing more ANYFILE.EXT on the UNIX system, and occasionally did that on DOS. So, I used the DOS REN command to rename MORE.EXE to DOSMORE.EXE. Then, when I ran my version of More it did not clash with the existing DOS program of the same name. If I want to use the original DOS MORE program, I can still enter a command such as TYPE ANYFILE.EXT | DOSMORE.

The moral of the story is that users expect certain results when they do certain things. It is hard for them to remember that the same command does different things depending on the context. Sometimes users can work around the problem by renaming commands or creating aliases, but they shouldn't have to. Whenever you write a program that has a direct interface to a human user, be sure you are consistent with what that user is accustomed to.

4.2.1 Multiple Loop Exits

Respectable loops have one entry and one exit point. To be perfectly proper the exit point is at the beginning or end of the loop. Some people allow the exit point to be in the middle of the loop, but they do so at the risk of being snubbed by elite programmers. A close inspection of Listing 61 shows that it ends with a loop containing (shudder!) two exit points, and one of them is in the middle. How could anyone with a name like Do-While do such a thing?

The software guideline restricting all loops to Do- While or Repeat-Until structures has merit. Certainly I'm not advocating a return to long loops full of GOTOs that twist a tangled trail through tortuously tightened tentacles. Those structures are as hard to understand as they are to read. Loops that enter at the top and exit only at the top or the bottom are much easier to understand and maintain than something that looks like a nervous giant squid.

I am prepared to argue, however, that there are special instances when the most maintainable loop has multiple exits. Furthermore, I submit that the More program is one such instance. I think it is much cleaner than the single exit from the nested named loops in the Show program.

The two-exit loop in the More program more closely describes what is really happening than the nested loops in the Show program do. Specifically, it displays some lines of text on the screen, and if it has displayed all there are to display, it quits. If there are more lines left, it lets the user decide how many more he wants to see. If he doesn't want to see any more, the loop ends. The loop is so short, it is easy to find all the exit points.

If I had coded the Display and get routines in-line instead of using procedure calls, then I would agree that the two exits are hard to find, and would submit to any number of lashes with a wet noodle. Instead, the Display routine hides the confusing fact that there is another loop reading and writing one line at a time. The Show procedure might have less trouble passing a code walkthrough, but everyone should agree, More is better.


Contents | Next ...