|
1 | 1 | # 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. |
3 | 5 |
|
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. |
7 | 27 |
|
8 | 28 | Examples:
|
9 | 29 |
|
10 | 30 | n = (maxread-minread) / binwidth2 + 1;
|
| 31 | + |
11 | 32 | bins(z) = bins(z-1) + width;
|
12 | 33 |
|
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 | + } |
15 | 52 |
|
16 | 53 | #### 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. |
19 | 57 |
|
20 |
| -Examples: |
| 58 | +Examples: |
21 | 59 |
|
22 | 60 | if (this > that)
|
| 61 | + |
23 | 62 | for (f = 1; f <= number; f++)
|
24 | 63 |
|
25 |
| -## TPL |
| 64 | + |
| 65 | +## TPL Sections |
26 | 66 | 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. |
27 | 71 |
|
28 | 72 | ### 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. |
32 | 77 |
|
33 | 78 | Examples:
|
34 | 79 |
|
35 | 80 | vector length(1,nlength);
|
| 81 | + |
36 | 82 | ivector parmlist(1,2*number);
|
37 | 83 |
|
38 | 84 | ### 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. |
0 commit comments