J. Blustein

Teaching Materials Archive

About Software Coding (by J. Blustein)

Common Mistakes & Marking Codes (BGSU edition)

This list of mistakes commonly seen in undergraduate computer science programming assignments is based on a version I used while teaching introductory C++ at Bowling Green State University.

The mistakes are grouped into the following 5 categories:


Comments

1.
  1. Comment briefly on what your program does.
  2. Comment on what each function does.
2.
  1. Comment the `logical sections' of your code. Be brief but precise.
  2. Do not restate what the source code says but rather explain what it's purpose is.
3. Comment on what your variables represent.

Readability

4. Use meaningful variable and function names.
5. Avoid `magic numbers'. Use meaningfully named const variables instead.
6.
  1. Indent consistently for readability.
  2. Seperate sections of your program with blank lines.
7. Do not unnecessarily repeat code in a program. Implement repeated tasks as functions or loops.
8. Use modularity: main() should mostly just call the functions that are the sub-modules of the program.

Clear coding

9. Do not include unnecessary code.
Do not initialize unnecessarily — it's confusing.
10. Use appropriate conditional structures (e.g., if or if/else or switch).
11.
  1. Use the appropriate loop structure
    • Use a while loop when you want to test the condition before you enter the loop the first time.
    • Use a do/while loop when you want to execute the loop at least once.
  2. Use a for loop only when you know how many times you want to repeat the body of the loop.
  3. Don't use break to jump out of loops.
    • (This was a department policy for the first C++ course, for an opinion about how break can make some loops clearer see The Practice of Programming by Kernighan & Pike.)
12. Use parameters instead of global variables to pass information to (and from) functions.
13. Do not use unnecessary parameters.

Output

14. Prompts to the user should be explicit, and helpful to the user.
15. Format your output for readability.
16.
  1. Submit all your output.
  2. Test runs should be executed in the order presented in the assignment.

Other

17. Check your spelling.
18. main() always returns an int.
  • Unless you are passing arguments on the command line, use int main(void).

See Also


http://www.cs.dal.ca/~jamie/teach/JBlustein/Coding/BGSUCS205.html

(This document is written in valid XHTML 1.0)

Last updated on 27 January 2005 by J. Blustein.