You are here

General Discussion

6 February, 2015 - 09:30

We are going to consider a simple program that might be used for testing a compiler to make sure that it is installed correctly.

Example 6.4: Compiler Test.cpp source code

//****************************************************** // Filename: Compiler Test.cpp // Purpose: Average the ages of two people // Author: Ken Busbee; © Kenneth Leroy Busbee // Date: Jan 5, 2009 // Comment: Main idea is to be able to // debug and run a program on your compiler. //****************************************************** 

// Headers and Other Technical Items 

#include <iostream> using namespace std; 
// Function Prototypes 

void pause(void); 

// Variables 

int agel; int age2; double answer; 

//******************************************************// main //*****************************************************
int main(void) { // Input cout « "\nEnter the age of the first person --->: "; cin » agel; cout « "\nEnter the age of the second person -->: "; cin » age2; 
// Process answer = (agel + age2) 1 2.0; 
// Output cout « "\nThe average of their ages is -------->: "; cout « answer; 
pause(); return 0; } 

//****************************************************** // pause //****************************************************** 
void pause(void) { cout « "\n\n"; system("PAUSE"); cout « "\n\n"; return; } 

//****************************************************** // End of Program //****************************************************** 

Within the programming industry there is a desire to make software programs easy to maintain. The desire centers in money. Simply put, it costs less money to maintain a well written program. One important aspect of program maintenance is making source code listings clear and as easy to read as possible. To that end we will consider the following:

  1. Documentation
  2. Vertical Alignment
  3. Appropriate use of Comments
  4. Banners for Functions
  5. Block Markers on Lines by Themselves
  6. Indent Block Markers
  7. Meaningful Identifier Names Consistently Typed
  8. Appropriate use of Typedef

The above items are not needed in order for the source code to compile. Technically the compiler does not read the source code the way humans read the source code. But that is exactly the point; the desire is to make the source code easier for humans to read. You should not be confused between what is possible (technically will compile) and what is ok (acceptable good programming practice that leads to readable code). Let's cover each item in more detail.