2014-02-27 15:57:22 +00:00
|
|
|
Coding Style
|
|
|
|
============
|
|
|
|
|
|
|
|
Here are some of the basics that we are trying to enforce for our coding
|
|
|
|
style. The existing code (as of the commit that adds these lines) is not
|
|
|
|
yet fully consistent to these rules, but following these rules will make
|
|
|
|
sure that no one yells at you about your patches.
|
|
|
|
|
|
|
|
We have a script that can be used to reformat code to be reasonably close
|
|
|
|
to these rules; it's in scripts/whitespace.pl - this script requires
|
|
|
|
clang-format to be installed (which sadly isn't installed by default on
|
|
|
|
any of our platforms; even on Mac where clang is the default compiler).
|
|
|
|
|
|
|
|
At the end of this file are some ideas for your .emacs file (if that's
|
|
|
|
your editor of choice) as well as for QtCreator. If you have settings for
|
|
|
|
other editors that implement this coding style, please add them here.
|
|
|
|
|
|
|
|
Basic rules
|
|
|
|
===========
|
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
- all indentation is tabs (set to 8 char) with the exception of
|
|
|
|
continuation lines that are alligned with tabs and then spaces
|
|
|
|
|
|
|
|
- all keywords followed by a '(' have a space in between
|
|
|
|
|
|
|
|
if (condition)
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
|
|
|
|
- function calls do NOT have a space between their name and argument
|
|
|
|
|
|
|
|
i = some_function(argument);
|
|
|
|
|
|
|
|
- usually there is no space on the inside of parenthesis (see examples
|
|
|
|
above)
|
|
|
|
|
|
|
|
- function / method implementations have their opening curly braces in
|
|
|
|
column 1
|
|
|
|
|
|
|
|
- all other opening curly braces follow at the end of the line, with a
|
|
|
|
space separating them:
|
|
|
|
|
|
|
|
if (condition) {
|
|
|
|
dosomething();
|
2015-03-18 00:22:32 +00:00
|
|
|
dosomethingelse();
|
2014-01-16 04:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- both sides of an if / else clause either use or do not use curly braces:
|
|
|
|
|
|
|
|
if (condition)
|
|
|
|
i = 4;
|
|
|
|
else
|
|
|
|
j = 6;
|
|
|
|
|
|
|
|
if (condition) {
|
|
|
|
i = 6;
|
|
|
|
} else {
|
|
|
|
i = 4;
|
|
|
|
j = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
- use space to make visual separation easier
|
|
|
|
|
|
|
|
a = b + 3 + e / 4;
|
|
|
|
|
|
|
|
- continuation lines have the operator / comma at the end
|
|
|
|
|
|
|
|
if (very_long_conditiont_1 ||
|
|
|
|
condition_2)
|
|
|
|
|
|
|
|
b = a + (c + d +
|
|
|
|
f + z);
|
|
|
|
|
2014-01-22 16:25:14 +00:00
|
|
|
- in a C++ constructor initialization list, the colon is on the same line and
|
|
|
|
continuation lines are aligned as the rule above:
|
|
|
|
|
2015-03-23 12:54:16 +00:00
|
|
|
ClassName::ClassName() : x(1),
|
|
|
|
y(2),
|
|
|
|
z(3)
|
2014-01-22 16:25:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
- unfortunate inconsistency:
|
|
|
|
-- C code usually uses underscores to structure names
|
|
|
|
|
|
|
|
variable_in_C
|
|
|
|
|
|
|
|
-- C++ code usually uses camelCase
|
|
|
|
|
|
|
|
variableInCPlusPlus
|
|
|
|
|
|
|
|
where the two meet, use your best judgment and go for best consistency
|
|
|
|
(i.e., where does the variable "originate")
|
2014-01-16 05:58:06 +00:00
|
|
|
|
|
|
|
- switch statements with blocks are a little bit special (to avoid indenting
|
|
|
|
too far)
|
|
|
|
|
|
|
|
switch (foo) {
|
|
|
|
case FIRST:
|
|
|
|
whatever();
|
|
|
|
break;
|
|
|
|
case SECOND: {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
do_something(i);
|
|
|
|
}
|
2014-01-22 16:25:14 +00:00
|
|
|
}
|
2014-02-27 15:57:22 +00:00
|
|
|
|
2014-07-16 16:16:47 +00:00
|
|
|
- variable declarations
|
|
|
|
in C code we really like them to be at the beginning of a code block,
|
|
|
|
not interspersed in the middle.
|
|
|
|
in C++ we are a bit less strict about this - but still, try not to go
|
|
|
|
crazy.
|
|
|
|
|
2014-12-17 09:23:53 +00:00
|
|
|
- text strings
|
|
|
|
The default language of subsurface is US English so please use US English
|
|
|
|
spelling and terminology.
|
|
|
|
Where at all possible strings should be passed to the tr() function to enable
|
|
|
|
translation into other languages.
|
|
|
|
|
|
|
|
-- like this
|
|
|
|
QString msgTitle = tr("Submit user survey.");
|
|
|
|
|
|
|
|
-- rather than
|
|
|
|
QString msgTitle = "Submit user survey.";
|
|
|
|
|
|
|
|
|
|
|
|
- UI text style
|
|
|
|
These guidleines are designed to ensure consitency in presentation within
|
|
|
|
Subsurface.
|
|
|
|
Only the first word of multi-word text strings should be captalized unless
|
|
|
|
a word would normally be capitalized mid-sentance, like Africa. This applies
|
|
|
|
to all UI text including menus, menu items, tool-tips, button text and label
|
|
|
|
text etc. e.g. "Check for updates" rather than "Check for Updates".
|
|
|
|
We also captialize Subsurface (NOTE: not SubSurface) when referring to the
|
|
|
|
application itself.
|
|
|
|
Abbreviations should end with a period, e.g. "temp." not "temp" for
|
|
|
|
temperature
|
|
|
|
Numerals in chemical formulae should use subscript characters e.g. O₂ not O2
|
|
|
|
Partial pressures in Subsurface are, by convention, abbreviated with a single
|
|
|
|
"p" rather than 2, as in pO₂ not ppO₂
|
|
|
|
Where more than one term exists for something, please choose the one already
|
|
|
|
in use within Subsurface e.g. Cylinder vs. Tank.
|
|
|
|
|
|
|
|
|
2014-02-27 15:57:22 +00:00
|
|
|
Sample Settings
|
|
|
|
===============
|
|
|
|
|
|
|
|
Emacs
|
|
|
|
-----
|
|
|
|
|
|
|
|
These lines in your .emacs file should get you fairly close when it comes
|
|
|
|
to indentation - many of the other rules you have to follow manually
|
|
|
|
|
|
|
|
;; indentation
|
|
|
|
(defun c-lineup-arglist-tabs-only (ignored)
|
|
|
|
"Line up argument lists by tabs, not spaces"
|
|
|
|
(let* ((anchor (c-langelem-pos c-syntactic-element))
|
|
|
|
(column (c-langelem-2nd-pos c-syntactic-element))
|
|
|
|
(offset (- (1+ column) anchor))
|
|
|
|
(steps (floor offset c-basic-offset)))
|
|
|
|
(* (max steps 1)
|
|
|
|
c-basic-offset)))
|
|
|
|
|
|
|
|
(add-hook 'c-mode-common-hook
|
|
|
|
(lambda ()
|
|
|
|
;; Add kernel style
|
|
|
|
(c-add-style
|
|
|
|
"linux-tabs-only"
|
|
|
|
'("linux" (c-offsets-alist
|
|
|
|
(arglist-cont-nonempty
|
|
|
|
c-lineup-gcc-asm-reg
|
|
|
|
c-lineup-arglist-tabs-only))))))
|
|
|
|
|
|
|
|
(add-hook 'c-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(let ((filename (buffer-file-name)))
|
|
|
|
;; Enable kernel mode for the appropriate files
|
|
|
|
(setq indent-tabs-mode t)
|
|
|
|
(c-set-style "linux-tabs-only"))))
|
|
|
|
|
|
|
|
(add-hook 'c++-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(let ((filename (buffer-file-name)))
|
|
|
|
;; Enable kernel mode for the appropriate files
|
|
|
|
(setq indent-tabs-mode t)
|
|
|
|
(c-set-style "linux-tabs-only"))))
|
|
|
|
|
|
|
|
|
|
|
|
QtCreator
|
|
|
|
---------
|
|
|
|
|
|
|
|
These settings seem to get indentation right in QtCreator. Making TAB
|
|
|
|
always adjust indent makes it hard to add hard tabs before '\' when
|
|
|
|
creating continuing lines. Copying a tab with your mouse / ctrl-C and
|
|
|
|
inserting it with ctrl-V seems to work around that problem (use Command
|
|
|
|
instead of ctrl on your Mac)
|
|
|
|
|
|
|
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE QtCreatorCodeStyle>
|
|
|
|
<!-- Written by QtCreator 3.0.0, 2014-02-27T07:52:57. -->
|
|
|
|
<qtcreator>
|
|
|
|
<data>
|
|
|
|
<variable>CodeStyleData</variable>
|
|
|
|
<valuemap type="QVariantMap">
|
|
|
|
<value type="bool" key="AlignAssignments">false</value>
|
|
|
|
<value type="bool" key="AutoSpacesForTabs">false</value>
|
|
|
|
<value type="bool" key="BindStarToIdentifier">true</value>
|
|
|
|
<value type="bool" key="BindStarToLeftSpecifier">false</value>
|
|
|
|
<value type="bool" key="BindStarToRightSpecifier">false</value>
|
|
|
|
<value type="bool" key="BindStarToTypeName">false</value>
|
|
|
|
<value type="bool" key="ExtraPaddingForConditionsIfConfusingAlign">false</value>
|
|
|
|
<value type="bool" key="IndentAccessSpecifiers">false</value>
|
|
|
|
<value type="bool" key="IndentBlockBody">true</value>
|
|
|
|
<value type="bool" key="IndentBlockBraces">false</value>
|
|
|
|
<value type="bool" key="IndentBlocksRelativeToSwitchLabels">false</value>
|
|
|
|
<value type="bool" key="IndentClassBraces">false</value>
|
|
|
|
<value type="bool" key="IndentControlFlowRelativeToSwitchLabels">true</value>
|
|
|
|
<value type="bool" key="IndentDeclarationsRelativeToAccessSpecifiers">true</value>
|
|
|
|
<value type="bool" key="IndentEnumBraces">false</value>
|
|
|
|
<value type="bool" key="IndentFunctionBody">true</value>
|
|
|
|
<value type="bool" key="IndentFunctionBraces">false</value>
|
|
|
|
<value type="bool" key="IndentNamespaceBody">false</value>
|
|
|
|
<value type="bool" key="IndentNamespaceBraces">false</value>
|
|
|
|
<value type="int" key="IndentSize">8</value>
|
|
|
|
<value type="bool" key="IndentStatementsRelativeToSwitchLabels">true</value>
|
|
|
|
<value type="bool" key="IndentSwitchLabels">false</value>
|
|
|
|
<value type="int" key="PaddingMode">2</value>
|
|
|
|
<value type="bool" key="SpacesForTabs">false</value>
|
|
|
|
<value type="int" key="TabSize">8</value>
|
|
|
|
</valuemap>
|
|
|
|
</data>
|
|
|
|
<data>
|
|
|
|
<variable>DisplayName</variable>
|
|
|
|
<value type="QString">Subsurface</value>
|
|
|
|
</data>
|
|
|
|
</qtcreator>
|
2014-02-27 21:42:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
Vim
|
|
|
|
---------
|
|
|
|
|
2014-02-28 09:09:18 +00:00
|
|
|
As everybody knows vim is a way better editor than emacs and thus needs to be
|
2014-05-10 16:34:34 +00:00
|
|
|
in this file too. Put this into your .vimrc and this should produce something
|
2014-02-27 21:42:10 +00:00
|
|
|
close to our coding standards.
|
|
|
|
|
|
|
|
" Subsurface coding style
|
|
|
|
filetype plugin indent on
|
|
|
|
filetype detect
|
2014-03-10 17:19:40 +00:00
|
|
|
set cindent tabstop=8 shiftwidth=8 cinoptions=l1,:0,(0,g0
|
2014-02-27 21:42:10 +00:00
|
|
|
" TODO: extern "C" gets indented
|
|
|
|
|
|
|
|
" And some sane defaults, optional, but quite nice
|
|
|
|
set nocompatible
|
|
|
|
syntax on
|
|
|
|
colorscheme default
|
2014-03-10 17:19:40 +00:00
|
|
|
set hls
|
|
|
|
set is
|
2014-02-27 21:42:10 +00:00
|
|
|
|
|
|
|
" The default blue is just impossible to see on a black terminal
|
|
|
|
highlight Comment ctermfg=Brown
|
|
|
|
|
|
|
|
" clearly point out when someone have trailing spaces
|
|
|
|
highlight ExtraWhitespace ctermbg=red guibg=red
|
|
|
|
|
|
|
|
" Show trailing whitespace and spaces before a tab:
|
|
|
|
match ExtraWhitespace /\s\+$\| \+\ze\t/
|