Next: Checkoff
Up: Good Style
Previous: PARAMETER Statements for Named Constants

Two Examples

Now of course, all of this may make sense to you, or perhaps it doesn't seem important. Also, its hard to know what ``good style'' is until you've seen examples of both good and bad style. I have prepared two programs that perform computations based on the ideal gas law, PV=nRT, which describes the relationship between pressure, volume, mass (``moles'' in chemistry) and temperature of an ideal gas. (R is a constant.)

The two programs perform all the same computations, with the sole exception of style. First, take a look at the program with no style, ``ugly-gas.f'' (or view it directly) .

Yuck!! What the heck does it do? I hope you agree its pretty hard to tell by looking at it. You'll notice I've removed all the spaces (except within the quotation marks for output statements), since FORTRAN doesn't care about the spaces. I've also used really grungy variable names like x9889 and x9898, with implicit typing, no comments and no indentation. I hope you'll agree that this is a very ugly program! (Now, put yourself in your grader's shoes and imagine grading 25 programs like this....)

The ``ideal'' version of the same program is in ``ideal-gas.f'' or you can view it directly.

This program is much longer, and although it does exactly the same computation as the first program, I hope you also agree that it's much easier to read.

Even so, there are ``style'' judgments in this program. I didn't define PARAMETERS for the magic number menu choices; I just used the hard-coded numbers. This was a judgment call, but it was influenced by the fact that FORTRAN doesn't let you define a PARAMETER that can be used in both the MENU function and the main program block. There are ways around this shortcoming of FORTRAN, but you needn't concern yourself with it.


Next: Checkoff
Up: Good Style
Previous: PARAMETER Statements for Named Constants

Hamlet Project
hamlet@cs.utah.edu