Re: The Principles of Good Programming
Posted: Aug 30, 2016 1:51 PM
Here is my advice to my high school students. Alas, it takes experience to have opinions about such advice, and high school students don't yet have that experience.
Advice for Developing Programmers
1. Limit functions to a single task, usually.
2. Label and align your output.
3. Document your programs at the top: name, date, class period (maybe course and instructor),
title, and program description. Watch your spelling, grammar, and punctuation.
4. Code with line numbers and never indent less than three spaces.
5. Use vertical alignment in your code, if it will emphasize significant relationships.
6. Do not use reserved words for identifiers or file names—e.g., random, max, print, etc.
7. Refactor (= redesign) your programs after they work to be more readable (IMPORTANT!).
8. Use step-wise refinement.
9. Write self-documenting code (descriptive identifiers, usually verb-object function names)
and thereby minimize comments.
10. Always print the run time, and maybe some other statistics, for every program.
11. Avoid magic numbers, unless they make the code significantly easier to understand.
12. Avoid global variables, but global constants are OK.
13. Do not write clever code (code that doesn’t immediately look like what it does), when
simpler code will do.
14. Choose readability over both optimization for speed and optimization for memory use.
15. Anticipate bugs by using defensive measures (asserts and error traps), but don’t overdo it.
16. Test every function upon completion (the golden rule). Untested code is broken code.
17. For a complicated algorithm, consider writing the tests before you write the code, and
consider running hundreds of randomly generated tests.
* * *
18. Do not start on the next assignment/function until you have finished the previous one.
19. When coding you need total focus. Avoid the chatty classmate.
20. Save every assignment on at least two different physical devices.
* * *
21. Write some code every week. Do not regress.
22. Hang around smart people and try to get them to talk shop.
23. Read the code of other programmers.
24. Try hard to avoid cheating.
25. Come to your problems with a history of attempting challenging problems.
26. Do not let schooling, grades, and outside activities sabotage your education. You are
responsible for your learning, not the school.