Matt's Programming Guidelines Version 1.1
(or What I Have Learned the Hard Way)
General
- Before starting, determine what is the problem you are trying to solve (This is not as obvious as it sounds).
- Think before you code.
- Think again.
- Think about how you are going to test your code.
- When possible, build test drivers into your code.
- Test your code as you implement.
- Test your code when you finish.
- Test boundary conditions.
- Backup your work using a variety of methods and media -- both code and documentation (and test the backups!)
- Having a legible printed copy of your design, code and documentation is always a good failsafe backup method.
- Comment the purpose of the code not the implementation (unless the code is doing something special).
- Try not to write code that does something special.
- Comment special coding techniques for the next person.
- When possible, re-use rather than re-write. That said, sometimes the only solution is to re-write.
- When estimating the time for a task, don't forget to include time for design, documentation and testing.
- Sad but true - before giving the estimate to your management, double the number and up the unit (e.g. 1 hour ==> 2 days).
- The software you write is a tool to solve someone's problem and not a means to showcase your brillance.
- Get the software to work before trying to optimize it.
- Every enhancement added is more code to test and document.
C Language
- Multi-line macros are strongly discouraged.
- Do not embed C keywords (exit, return, for, etc.) in macros.
- Each usage of a macro parameter must be parenthesised when used.
- Overall replacement text must be enclosed in ()s for expressions and do { ... } while (0) for statements.
- Use #define to give mnemonic names to constant expressions.
- Do not create ad hoc syntax extensions.
- Pointers can only point to: NULL, another pointer, &variable, an array, a function name, and a pointer valued function call.
- Don't use >> on signed quantities.
- Never extract data from a variable in a manner other then that in which it was stored.
- Use typedef to create names for commonly used types.
- Every function and variable must be explicitly typed.
- Whenever reasonable, declare functions returning int, not short or char.
- Functions that do not return a value should be declared as type void.
- Expressions in while, for, if and ?: should have boolean values.
- Use int and double as generic types when space doesn't matter.
- Do not assume that a pointer and a long are interchangeable.
- Never depend upon the order of bytes in a variable.
- Use ctype rather than compares or bit checks on characters.
- Do not dpened on results of arithmetic expressions on char values.
- Only compare characters using == and !=.
- Always pass structures by reference using a pointer; do not pass structures by value.
- Do not use gotos.
- for (;;) is usually better (and faster) than while (1).
- Do not declare public variables in the #include file.
- If a constant is used only in one module it should be defined at the head of the module.
- In general, avoid nesting #include files.
- Keep source lines under 80 columns when possible.
- Use two (2) character indents.
- Try to order parameters as input only, input-output, and output only.
- Whenever possible, do not use break or continue inside loops.
- Null (empty) statements must be comments (e.g. /* do nothing */ ;).
- Space before and after each relational operator.
- Try to keep functions to a couple of pages in length; use sub-functions if necessary.