eTTe
Good Practice in Computer Science
Note that this page will be a constant work in progress. Good practices will continually be added and refined as they are introduced and discussed among students and teachers. These are written in the spirit of good programming practices. However, some of these tips may improve your test scores by helping the grader read and understand the code that you really meant to create.
Follow the instructions. Whether you are taking an exam or writing professional code, it does no good to create the perfect solution to the wrong problem. Make sure you understand what is being asked, or specified, before you jump into writing code. |
Indent your work, and use curly braces liberally. If you don't use these, and your code is legal, it SHOULDN'T hurt. However, if your code is difficult to read or understand, you have missed one of the most important parts about programming: maintainability (and someone grading your exam may simple not figure out what you did). |
-- The following is legal code, but might not get evaluated correctly due to its form: |
if( answer != testedValue )
cout << "The answer is not equal to: " << testedValue
<< " in this part of the code." << endl;
else cout << "The answer is equal to: " << testedValue
<< " at this point in the code, and the result is equal to: "
<< result << endl;
-- The following would not only be easier to read and generate fewer potential mistakes, it is also so much easier to evaluate that anyone reviewing your work will understand it: |
if( answer != testedValue )
{
cout << "The answer is not equal to: " << testedValue
<< " in this part of the code." << endl;
}
else
{
cout << "The answer is equal to: " << testedValue
<< " at this point in the code, "
<< "and the result is equal to: "
<< result << endl;
}
NOTE: Yes, it consumes more paper, but guess how the REAL programmers keep from getting themselves messed up on line number 145,729 of their code? |
When writing a function, write the header, and add both the opening and closing curly braces before doing anything else. When a curly brace is missing, some compilers can come up with the most obscure and confusing messages, which do not help you find the problem very easily. Always do this. It will save you hours of diagnosing compiler errors. |
int myNewFunction( int myParameter1, double myParameter2 )
{
}
AFTER you have typed the function header and the curly braces, THEN start filling in your comments, your pseudocode, and finally, your program code.
Do not use global variables. There is rarely ever any use for global variables; and they should not be used if at all possible. On the other hand, global constants are very appropriate. As a secondary note, it is common to capitalize the constant quantities in a program. Examples: |
In C++,
const int MAX_SCREEN_WIDTH = 80;
const char TAB = '\t';
or, inside a Java class
final int DAYS_IN_WEEK = 7;
Do not use literal values in the program code. In most cases, numerical or character constants are rarely used in professional programming. The constants zero (0) and one (1) are commonly used in program code, but virtually all other constants should be declared as constants at the beginning of the program. Examples: |
if(
x > 80 )
// bad form
//
what happens if your program is to be used on a computer
//
with a screen size of 40?
if(
x > MAX_SCREEN_WIDTH ) //
proper form
//
if the value needs to be changed, it only has to be changed
//
in one place in the program
if(
myChar == 'q' )
// bad form
//
How will another programmer know what 'q' is for?
if(
myChar == QUIT_CHAR ) //
proper form
// it is very clear what this character is to be used for
Always compile after entering three to five lines of code, or after every small function. |
If you compile frequently, you will know what changed since the last time you compiled, and you will be able to identify small syntax problems quickly and easily.
void myFirstFunction()
{
// code typed
}
COMPILE
void mySecondFunction()
{
// code typed
}
COMPILE
And so on . . .