3 | | Use the K&R style and indent using four spaces instead of tab. Avoid lines over 80 characters long, though this is not strict where there are multiple indents. |
| 3 | Rules: |
| 4 | * K&R brace style, with four spaces per indent |
| 5 | * Avoid lines over 80 characters long (not strict if there are multiple indents, but ideally they should be refactored) |
| 6 | * If a function takes no parameters, it should be declared as function(void), not just as function(). |
| 7 | * Use const where you shouldn't be modifying a variable. |
| 8 | * Avoid global variables like the plague, we already have too many. |
| 9 | * Use enums where possible instead of defines, and never use magic numbers. |
| 10 | * Don't use floating point. |
| 11 | * Code should compile as C89 and not rely on undefined behaviour. |
| 12 | * Don't use the C built-in string functions, use the my_ versions instead (strcpy -> my_strcpy, sprintf -> strnfmt()). They are safer. |
7 | 15 | * Opening braces should be on a separate line at the start of a function, but should otherwise follow the statement which requires them ('if', 'do', 'for' et al.) |
8 | 16 | * Closing braces for should be on separate lines, except where followed by 'while' or 'else' |
9 | 17 | * Spaces around the mathematical, comparison, and assignment operators ('+', '-', '/', '=', '!=', '==', '>', ...). No spaces around increment/decrement operators ('++', '--'). |
48 | | Write code as modularly as possible. Name functions and global variables inside a module with the same prefix, like "macro_". Avoid global variables where possible, and don't expose them to the rest of the code unnecessarily. |
49 | | |
50 | | If you make use of memory allocation in your module, try and make such code clean up after itself, or include it in "init" and "free" functions. Always initialise your module so it is in a known state! It is possible that the game will eventually not quit when you lose a character, but rather allow another character to be loaded or created. Unless you get things in a known state, this will cause crashes. |
51 | | |
52 | | Don't use magic numbers. Use defines or enums (and enums where possible). Where a constant is local, include it at the top of a file/section. Where it is not, place it in the module's corresponding header file. Avoid putting more things into defines.h/externs.h. |
53 | | |
54 | | Don't use floating point calculations. |
55 | | |
56 | | Code should compile cleanly as strict C89 on release and should not rely on undefined behaviour. (Angband has, for years, been a well-defined piece of software, and no-one implements C99 fully yet.) There are recent moves to allow mixed declarations and code, so you may see some of those warnings in recent versions. |
57 | | |
58 | | Never use `strcpy()`, `strcat()`, or `sprintf()`. Instead, use my_strcpy, my_strcat and strnfmt, which are safer and will avoid accidental trashing of memory. Similarly, avoid strncat/strncpy unless absolutely necessary, as using them is error-prone. |
| 58 | * You should write code as modules wherever possible, with functions and global variables inside a module with the same prefix, like "macro_". |
| 59 | * If you need to initialise stuff in your module, include "init" and "free" functions and call them appropriately rather than putting module-specific stuff all over the place. |
| 60 | * One day the game might not quit when a game ends, and might allow loading other games. Keep this in mind. |