Code Style - dagelf/synergy-core GitHub Wiki
(tab)printf("hello world"); // correct
printf("hello world"); // incorrect
Currently we use tabs instead of spaces. However we are considering using 2 spaces instead of tabs in the future, since tab sizing can be changed (people often change their tab size to 2), which results in inconsistent tab sizes between different editors.
Always use *nix line feed (\n) for line endings. Never use Mac CR (\r) or Windows CR/LF (\r\n). Most of the time, git will handle this automagically.
As a courtesy to command line users (using cat/type/more/less commands), files should always end in a new line so that the command prompt looks tidy.
class MyClass {
public:
Foobar m_foobar;
Foobar foobar();
ReallyReallyLongClassName
m_memberOnNextLine;
};
MyClass::MyClass(
Foobar* foobar,
MoreStuff* moreStuff) :
m_foobar(foobar)
{
// ...
}
class MyClass { };
struct MyStruct { };
enum EMyEnum { }; // enum prefix is E
class MyClass {
public:
void helloWorld();
};
class MyClass {
public:
MyClass* m_myClass1;
private:
MyClass* m_myClass2;
};
enum EMyEnum {
kValue1,
kValue2
};
static MyClass* s_myClass;
// i'm using bad grammar. but i like to use full stops
DEBUG((CLOG_INFO "hello world"));
// for long block comments, instead of using the slash asterisk
// comments, we use the two-slash comments
// correct
int
MyClass::helloWorld()
{
}
// incorrect
int MyClass::helloWorld()
{
}
// correct
int
MyClass::helloWorld()
{
// ...
}
// incorrect
int
MyClass::helloWorld() {
// ...
}
// correct
if (a == b) {
// ...
}
// correct
for (int i = 0; i < a; i++) {
// ...
}
// incorrect
if (a == b)
{
// ...
}
// incorrect
for (int i = 0; i < a; i++)
{
// ...
}
// correct
a = b;
if (c == d) {
}
// incorrect
a=b;
if (c==d) {
}
// correct
if (a == b) {
// ...
}
// incorrect
if ( a == b ) {
// ...
}
// correct
void helloWorld();
// incorrect
void helloWorld(void);
class MyClass {
public:
const char* helloWorld();
};
MyClass:: MyClass(const char* helloWorld)
{
}
// correct
if (a == b) {
}
// incorrect
if(a == b) {
}
// correct
char* helloWorld;
// incorrect
char *helloWorld;
// correct
#if HELLO_WORLD
# include "HellWorld.h"
#endif
// incorrect
#if HELLO_WORLD
#include "HellWorld.h"
#endif
// correct
MyClass* myClass1 = new MyClass();
MyClass& myClass2 = *myClass1;
// incorrect
MyClass* p_myClass1 = new MyClass();
MyClass& p_myClass2 = *p_myClass1;
// incorrect
MyClass* pMyClass1 = new MyClass();
MyClass& pMyClass2 = *pMyClass1;
// correct
if (a == "hello world") {
}
// incorrect
if ("hello world" == a) {
}
// correct
if (a == b) {
// ...
}
else {
// ...
}
// incorrect
if (a == b) {
// ...
} else {
// ...
}
void
foobar() {
functionWithManyArgs(
arg1,
arg2,
arg3);
}
switch (type) {
case kFoobar:
break;
}
case kFoobar: {
// ...
break;
}
// correct
#include "arch/win32/ArchInternetWindows.h"
// incorrect
#include "ArchInternetWindows.h"
#include "arch/win32/ArchInternetWindows.h" // matching source/header pair
#include "arch/win32/XArchWindows.h" // arch/win32 could depend on arch
#include "arch/Arch.h" // arch could depend on base
#include "base/Log.h" // base could depend on common
#include "common/Version.h" // probably has the least dependencies
#include <sstream> // system (after divide)
#include <Wininet.h> // system
Rationale: This ordering is to reduce accidental dependency solving,
so that headers are as independent as possible. For example, including a
system header (e.g. sstream
) before a local include (e.g.
ArchInternetWindows.h
) may accidentally solve a dependency (headers
should include their own dependencies). There should be a clear divide
between each group (pair, local and system) so that its easy to see the
pair header is first, and it also reduces the possibility of local
headers being “tagged on” to the end of the include list, which could
introduce accidental dependencies; someone who hasn't read this guide is
more likely to ask, “Why is there a divide here?”
// correct
#pragma once
// incorrect
#ifndef OLD_STYLE_GUARD_H
#define OLD_STYLE_GUARD_H
// ...
#endif // OLD_STYLE_GUARD_H
Rationale: #pragma once
is very well supported across compilers
but not actually part of the standard. The preprocessor may be a little
faster with it as it is more simple to understand your exact intent.
#pragma once
is less prone to making mistakes and it is less code to
type.
//
// correct
//
#include "foo/Bar.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
//
// incorrect
//
#define WIN32_LEAN_AND_MEAN
#include "foo/Bar.h"
#include <Windows.h>
Rationale: Though unlikely, local header (e.g. Bar.h
) may also
include Windows.h
but needs the features that the
WIN32_LEAN_AND_MEAN
macro removes. This is probably the worst example,
as you almost always want to use #define WIN32_LEAN_AND_MEAN
(except
for when using IUnknown
, for example).
// correct
#include "example/foo_bar.h" // no classes here!
# incorrect
#include "example/FooBar.h" // no classes here!
Rationale: Upper camel case naming for non-class names is potentially misleading, as it suggests that the file contains a class.
// correct
TcpFoobar m_tcpFoobar;
FoobarTcp m_foobarTcp;
# incorrect
TCPFoobar m_TCPFoobar;
FoobarTCP m_FoobarTCP;
Rationale: TCPFoobar could be misread as TCPF-oobar, and members are always lowerCamel.
// correct
return a == b;
// incorrect
if (a == b) {
return true;
}
else {
return false;
}
// correct
throw GreatDisturbanceInForce(
"millions of voices suddenly cried out in terror, "
"and were suddenly silenced");
throw MonkiesHaveTakenOverThePlanet(monkeyCount);
// incorrect
return 42; // the wrong question was asked
return -1; // there's a new virus in the database
return -1; // the rabbit is in the administration system
return -1; // too many garbage files, need more time
return -1; // expected cake, but there was none
// correct
enum EFoobarErrors {
kInvalidCapitalOfCountry,
kInvalidFavoriteColor
}
if (favoriteColor == kColorBlue) {
return kInvalidFavoriteColor;
}
// incorrect
if (capitalOfAssyria == NULL) {
return -1; // invalid capital of country
}