These tips are to help you learn the style I've developed over the years. There is a lot of code here, all written by myself (well, all but a few lines that is) and it all conforms to the standard I use. If you want to keep the code from looking like spaghetti as the years roll by, you should try to adhere to these standards too.
Programming in C or C++
Style
- Curly braces are left aligned, as so:
MyFunc() { ... }orif( blah == blah ) { ... } - 3 character tab stops: in vi you can do this with "se ts=3". Why 3? Because you need indents to make code readable, but you don't need whitespace like on a resume. 3 spaces is plenty. It also gives you more room on each line for code and still fall inside the next rule.
- 78 character line lengths max: Why? Because all code should be readable on any standard terminal and 80 characters is the max on any standard terminal.
- Comment blocks: Start every file and function with a block of
comments. The format of this block is not set in stone, but it should
describe the input and output arguments, if any, and any return values.
It's hard to keep these up to date, but it's important to try to make the
code maintainable. There are plenty of examples in the XNotesPlus
project, but here is what a typical comment block looks like (take from
xnotes.c in the XNotesPlus project):
/*======================================================================== * Name: ParseCmdLineOptions * Prototype: ParseCmdLineOptions(int argc, char **argv) * * Description: * Parse the command line for user specified options. These take * precedence over any values initialized by XNotesPlus. * * Input Arguments: * int arc number of arguments in argv * char **argv command line arguments * * Output Arguments: * Return Values: * Global Variables: * External Routines: * Method: * Restrictions: * Notes: * This routine requires getopt_long(). *========================================================================*/
- Do not use "//" notation in C++, use standard C comment blocks. It's too easy to use to lose track of those little comments used to swap between different bits of code while debugging. If you need to comment something out, use a comment block with dashes at the top of the block - make that comment stand out so we can pull it out later if necessary.
- Try not to ifdef the world to death. Ifdef is useful and it has its places, but if you find you have more than a few ifdefs in a module, then create a new module specific to the platform and ifdef the whole thing. It's cleaner, though not necessarily as easy to maintain.
Variable names
- No single letter variable names! If you need a variable, call it what it is, even if it takes 50 characters. Never call something "a" when you mean "supersizedarrayvalue". Cscope helps deal with this, but its still a pain.
- The required exception to the first rule: i,j,k or "count" for counters and incrementers.
- Scope properly: variables have meaning within certain scopes, such as
a function, a module or globally. Module variables should be statically
declared at the top of the module. Global variables should be defined in a
header file and include like so:
#ifdef C_MAIN int XP_MY_VAR; #else extern int XP_MY_VAR; #endif
Initialize variables in the header file only to 0 or NULL values. - Use WikiWord style for function names (capitilize first letter of words)
plus prefix with project specific prefix (XP for XNotesPlus, for example):
static int XPFunctionName(){...} - Global variables use project prefix and underscores, plus uppercase
first letters:
XP_Hello_World - Function variables use all lower case, no prefix and no underscores:
helloworld - Module scoped variables use uppercase, underscores but no prefix:
Hello_World - Defined values (including macros) are all uppercase with prefix and
underscores:
XP_HELLO_WORLD - typedefs are defined with lowercase and underscore prefix, but instanced
without underscare and use uppercase and suffixed with _T:
typedef struct _hello_world {...}HelloWorld_T;
Programming in Perl
Style
- curly braces left aligned
- single line code allowed - makes for easy reading and compact files.
- comment blocks
- define subroutines, then code
Variable names
Try to use similar naming conventions as with C/C++ code.
