Skip to content

Commit 2bb1028

Browse files
Merge pull request #372 from nmfs-stock-synthesis/163-new-style
163 new style
2 parents 9f66110 + 2f5f70e commit 2bb1028

File tree

3 files changed

+78
-21
lines changed

3 files changed

+78
-21
lines changed

.clang-format

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
---
22
Language: Cpp
3+
34
AccessModifierOffset: -4
45
AlignAfterOpenBracket: false
56
AlignConsecutiveAssignments: false
67
AlignEscapedNewlinesLeft: false
78
AlignOperands: false
89
AlignTrailingComments: false
910
AllowAllParametersOfDeclarationOnNextLine: true
10-
AllowShortBlocksOnASingleLine: true
11+
AllowShortBlocksOnASingleLine: false
1112
AllowShortCaseLabelsOnASingleLine: false
1213
AllowShortFunctionsOnASingleLine: Empty
13-
AllowShortIfStatementsOnASingleLine: true
14-
AllowShortLoopsOnASingleLine: true
14+
AllowShortIfStatementsOnASingleLine: false
15+
AllowShortLoopsOnASingleLine: false
1516
AlwaysBreakAfterDefinitionReturnType: None
1617
AlwaysBreakBeforeMultilineStrings: false
1718
AlwaysBreakTemplateDeclarations: false
1819
BinPackArguments: true
1920
BinPackParameters: true
2021
BreakBeforeBinaryOperators: None
21-
BreakBeforeBraces: Allman
22+
BreakBeforeBraces: Stroustrup
2223
BreakBeforeTernaryOperators: false
2324
BreakConstructorInitializersBeforeComma: true
2425
ColumnLimit: 0

coding_style.md

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,91 @@
11
# Draft programming style for SS3
2-
Please notice that the term C++ section is used as short-hand for code that is in the LOCAL_CALCS sections of the TPL code.
2+
SS3 is coded in C++ and ADMB TPL which have different requirements for compilation.
3+
C++ code is contained in 'LOCAL_CALCS' sections.
4+
The following is to enhance clarity while building using ADMB tools.
35

4-
## Math Expressions
5-
Generally, there is a space on each side of a mathematical operator (+ - * / =).
6-
This may be dispensed with when the expression is used as an index for an array, vector, matrix, etc.
6+
## LOCAL_CALCS Sections
7+
These sections are between the statements 'LOCAL_CALCS' and 'END_CALCS'
8+
each of which must be one space from the left.
9+
10+
If using clang-format, make
11+
sure these comments are at the beginning and ending of the section:
12+
// clang-format on, and
13+
// clang-format off.
14+
15+
Example:
16+
17+
LOCAL_CALCS
18+
// clang-format on
19+
. . .
20+
// clang-format off
21+
END_CALCS
22+
23+
24+
### Math Expressions
25+
Use spaces on each side of a mathematical operator (+ - * / =), unless
26+
the expression is used as an index for an array, vector, matrix, etc.
727

828
Examples:
929

1030
n = (maxread-minread) / binwidth2 + 1;
31+
1132
bins(z) = bins(z-1) + width;
1233

13-
## Control Statements
14-
There can be a space before the left parenthesis and after the right parenthesis. Brackets should always be used and put on their own line (so that the matching brackets are obvious).
34+
### Control Statements
35+
Use a space before the left parenthesis and after the right
36+
parenthesis in control statements (e.g. for, do while, while, if, else).
37+
38+
Use curly braces for all code blocks. For short blocks, they may be on the same line as the conditional statement, but for large blocks, put them on their
39+
own line (so that the closing curly brace is obvious).
40+
41+
Example:
42+
43+
if (a > b)
44+
{
45+
. . . // lots of code
46+
b = 2;
47+
. . . // more code
48+
}
49+
else {
50+
b = 4;
51+
}
1552

1653
#### Logic Expressions
17-
There is a space on each side of the logical operators (< > <= >= == !=).
18-
Math expressions used as indices may contain spaces or not depending on what makes it most clear.
54+
Use a space on each side of the logical operators (< > <= >= == !=).
55+
Logical expressions used as indices may contain spaces or not
56+
depending on what makes it most clear.
1957

20-
Examples:
58+
Examples:
2159

2260
if (this > that)
61+
2362
for (f = 1; f <= number; f++)
2463

25-
## TPL
64+
65+
## TPL Sections
2666
TPL sections deserve special consideration.
67+
While the LOCAL_CALCS section is treated as pure C++ code,
68+
the other sections of .tpl files are templates that are
69+
translated to C++ code before compilation, so there are
70+
additional syntax rules that must be taken into account.
2771

2872
### Indices
29-
Items with multiple indices cannot have spaces between them.
30-
In C++, spaces can be inserted, e.g. matrix1 (1, j, 5).
31-
In TPL, the same item would be matrix1(1,j,5). For the sake of consistency, this can be done in the C++ sections also.
73+
Items with multiple indices cannot have spaces between them.
74+
In C++, spaces can be inserted, e.g. matrix1 (1, j, 5) while in
75+
TPL, the same item would be matrix1(1,j,5). For the sake of
76+
consistency, this can be done in the C++ sections also.
3277

3378
Examples:
3479

3580
vector length(1,nlength);
81+
3682
ivector parmlist(1,2*number);
3783

3884
### Semi-colons
39-
Use semi-colons to end each statement especially if this will be
40-
passed through the "pretty_tpl" routine. Clang-format will join
41-
statements together if they are not separated by semi-colons.
85+
Use semi-colons to end each statement.
86+
Clang-format will join statements together if they are not separated
87+
by semi-colons.
88+
89+
## Indenting
90+
Use 2 spaces to indent each level of code to make it obvious what
91+
code belongs together.

pretty_tpl.bat

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ rem prettify tpl code by running through clang-format
55
rem and then tpl-format (to correct issues).
66
rem this assumes presence of clang-format.exe,
77
rem tpl-format.exe, and the file .clang-format
8+
rem
9+
rem if using LOCAL_CALCS/END_CALCS, clang-format can
10+
rem only work within those sections. Use // clang-format on
11+
rem to turn it on after a LOCAL_CALCS and // clang-format off
12+
rem to turn it off before END_CALCS for normal tpl code.
13+
rem (Also place a // clang-format off at the beginning of the code).
814
rem
9-
rem use: pretty_tpl filename
15+
rem usage: pretty_tpl filename
1016
rem
1117
echo formatting file %1
1218
clang-format -i %1

0 commit comments

Comments
 (0)