Tabs and Spacing
Two spaces (four is fine as well, just be consistent).Why? Never use tabs unless strictly necessary (inside a Makefile, etc.). Tabs are inconsistent and more often than not make a mess of things online when people browse code in repositories like Github. This is the most common way people browse our code, and spaces ensure that the code is consistent and readable in any context.
Naming Conventions
Learning from many of my own mistakes, these are the habits I've picked up over the years.File Names
Use all lower case letters!Why? While upper and lower case is a non-issue on Windows, it can cause endless headaches in other operating systems and build environments. Using only lower-case letters in your file names avoids this problem entirely, and your compiler will never complain that it can't find a file.
error_t pcf2129Init ( void ); void pcf2129SetCallback ( void (*pFunc)(void) ); error_t pcf2129ReadTime ( rtcTime_t *p_time ); error_t pcf2129SetTime ( rtcTime_t time ); error_t pcf2129SetInterrupt ( pcf2129_INTEvent_t eventFlags );
Field/Variable Names
Flag shared fields with '_' or 'm_' and use meaningful namesstatic bool _pcf2129Initialised = false; static void (*_pcf2129Callback)(void) = NULL;
You should also include the filename (or an abbreviation of it) in any global variables that you define like this to avoid problems debugging later when everything is linked together.
To make sure that people understand that they are working with pointers and 'references' to external memory blocks, prefix all pointer parameters with either 'p' or 'p_':
error_t pcf2129ReadTime ( rtcTime_t *p_time );
Indent Style
Use whichever you prefer between Allman and K&R, just be consistent!This is highly personal, but I've always been an Allman person, where the opening brace is on a newline:
/* Allman Style */ for ( i = 0; i < length; i++ ) { buffer[i] = I2CSlaveBuffer[i]; }
/* K&R Style */ for ( i = 0; i < length; i++ ) { buffer[i] = I2CSlaveBuffer[i]; }