Skip to content

PCem Code Style Guide

Michael Manley edited this page Apr 14, 2022 · 1 revision

This style guide is more for if you're using an editor other than CLion. Since I use CLion, I have shared the styling in the CLion Project (The .idea folder), so the editor will automatically adjust for PCem's coding style.

C/C++ Code

Most code follows now a modified LLVM style (https://releases.llvm.org/10.0.0/docs/CodingStandards.html). The major differences between the PCem Code Style to LLVM are as follows:

  • Tabs are 8 spaces instead of 4 in LLVM style.
  • Defines follow indentation, and #if/#ifdef lines are also indented.
  • namespaces contents are indented in (C++)
  • Class protections are indented 4 spaces (C++)

Also please follow the following other pointers

  • Try to avoid multiple statements on a single line unless it actually improves readability
  • Function names should be lower case with underscores
  • Global function names should include a sensible prefix (eg serial functions should start with serial_)
  • Avoid creating new global variables unless absolutely required
  • Prefer C89 "/* */" style comments. C99 "//" comments are generally used to comment out debug code
  • Try to avoid magic numbers, ie use #define or enum where appropriate
  • No hard limit on line lengths (prefer 150), but try to keep it sensible

See the following code to get an example.

#include <iostream>

namespace test {
	class StyleGuide {
	    public:
		StyleGuide();
		#ifdef TEST
			#define SUPERTEST
		#endif
	};
};

int main() {
	int i = 0;
	i++;
	i += 5;
	std::cout << "Here is a styleguide" << std::endl;
	if (i == 0 || i > 10) {
		std::cout << "OVER 10 OR 0!" << std::endl;
	} else {
		std::cout << "Hello!" << std::endl;
	}
}

CMake Files

PCem uses 8 tabs instead of 4. Also please use lowercase for commands and UPPERCASE for parameters. See below for an example.

function(f1)
        foreach(_VARIABLE ${_VARIABLES})
                message(${_VARIABLE})

                message(${_VARIABLE})
        endforeach()
        if(bar)
                message(STATUS "Bar")
        else()
                message(STATUS "Not bar!")
        endif()
endfunction()
Clone this wiki locally