dev:codingconventions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
dev:codingconventions [2016/03/16 13:10] – Added docu for new prettify pseewald | dev:codingconventions [2020/02/11 12:00] – oschuett | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Coding Conventions ====== | ====== Coding Conventions ====== | ||
+ | |||
===== Stick to the standard ===== | ===== Stick to the standard ===== | ||
- | | + | |
- | * Avoid using new OOP aspects, because compilers do not yet support them well enough. | + | |
- | * OpenMP code should follow | + | * OpenMP code should follow version 3.X of the standard |
- | * MPI should should follow | + | * MPI should should follow version |
- | * Extended functionality should match [[wp> | + | * Extended functionality should match [[wp> |
===== Write explicit code ===== | ===== Write explicit code ===== | ||
Line 15: | Line 16: | ||
* INTEGER i=4.9999, i.e. i=INT(4.9999) implies i==4. Use NINT(4.9999) as appropriate. | * INTEGER i=4.9999, i.e. i=INT(4.9999) implies i==4. Use NINT(4.9999) as appropriate. | ||
* natom*natom, | * natom*natom, | ||
+ | * always use REAL() with KIND parameter, i.e. r = REAL(i, KIND=dp). | ||
+ | * avoid FLOAT() in favour of REAL(, KIND=dp). | ||
* the global number of grid points (pw_grid_type%ngpts, | * the global number of grid points (pw_grid_type%ngpts, | ||
Line 36: | Line 39: | ||
===== Use existing infrastructure ===== | ===== Use existing infrastructure ===== | ||
- | For many common | + | |
+ | Always prefer [[https:// | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | For many common | ||
* Use the routines from '' | * Use the routines from '' | ||
* Use the routines from the full-matrix '' | * Use the routines from the full-matrix '' | ||
* Use the routines from '' | * Use the routines from '' | ||
+ | * Use the routines from '' | ||
* Use the routines from '' | * Use the routines from '' | ||
* Don't use '' | * Don't use '' | ||
Line 53: | Line 66: | ||
* Each module and routine should be annotated with [[dev: | * Each module and routine should be annotated with [[dev: | ||
* Each preprocessor flag should start with two underscores and be documented in the '' | * Each preprocessor flag should start with two underscores and be documented in the '' | ||
- | * The code should be formatted with the prettify-tool by running '' | + | * The code should be formatted with the [[dev: |
===== Write tests ===== | ===== Write tests ===== | ||
* Every feature should be tested, with the goal of complete [[ http:// | * Every feature should be tested, with the goal of complete [[ http:// | ||
Line 65: | Line 77: | ||
* Please run '' | * Please run '' | ||
* See our [[ http:// | * See our [[ http:// | ||
- | |||
- | ===== Formatting conventions and auto-formatting (prettify) ===== | ||
- | Uniform formatting of CP2K source code will be enabled in the near future by a prettify script that is an almost complete auto-formatter for Fortran 90 source code. As a rule of thumb, developers should not worry about the format of their code and just let prettify do its magic by running '' | ||
- | * Sorting and alignment of variable declarations and '' | ||
- | * Indentation with a relative width of 3 characters. | ||
- | * Line continuations are aligned with the previous opening delimiter '' | ||
- | * All operators are surrounded by exactly one whitespace character, except for arithmetic operators. | ||
- | * Removal of extraneous whitespace and consecutive blank lines. | ||
- | * Uppercase notation for all Fortran and OpenMP keywords. | ||
- | The following formatting decisions are still manual and are never changed by prettify: | ||
- | * Positions of line breaks (except for variable declarations and '' | ||
- | * No indentation of subsequent '' | ||
- | There may be cases where manual alignment is preferred over the rules stated above. The following options for manual formatting are provided: | ||
- | * No automatic realignment of line continuations that are prefixed with an ''&'' | ||
- | * No auto-formatting of lines to which a comment starting with '' | ||
- | * No auto-formatting of code blocks enclosed between two comment lines starting with '' | ||
- | |||
- | A few examples to illustrate how to deal with cases where auto-formatting produces unsatisfying results: | ||
- | |||
- | * Reduce hanging indent by inserting linebreaks directly after assignment operator and opening delimiter: | ||
- | <code fortran> | ||
- | ! No: | ||
- | long_result_var_name = long_function_name(arg_1, | ||
- | arg_3, arg_4, arg_5)+ & | ||
- | foo | ||
- | ! Yes: | ||
- | long_result_var_name = & | ||
- | | ||
- | arg_1, arg_2, & | ||
- | arg_3, arg_4, arg_5)+ & | ||
- | foo | ||
- | </ | ||
- | * Avoid linebreaks in deeply nested expressions: | ||
- | <code fortran> | ||
- | ! No: | ||
- | bessj0 = (r1+y*(r2+y*(r3+y*(r4+y* & | ||
- | (r5+y*r6)))))/ | ||
- | | ||
- | ! Yes: | ||
- | bessj0 = (r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/ | ||
- | | ||
- | </ | ||
- | * Alignment by explicit bracketing: | ||
- | <code fortran> | ||
- | ! No: | ||
- | foo = bar+foobar(x1, | ||
- | foobar(x2, y2, z2)* & | ||
- | foobar(x3, y3, z3) | ||
- | ! Yes: | ||
- | foo = bar+(foobar(x1, | ||
- | | ||
- | | ||
- | </ | ||
- | * Special vertical alignment may require manual formatting: | ||
- | <code fortran> | ||
- | ! Auto-formatting: | ||
- | align_me = [-1, 10, 0, & | ||
- | 0, 1000, 0, & | ||
- | 0, -1, 1] | ||
- | ! Manual alignment (!& disables whitespace formatting): | ||
- | align_me = [-1, 10, 0, & !& | ||
- | 0, 1000, 0, & !& | ||
- | | ||
- | ! Alternatively: | ||
- | !&< | ||
- | align_me = [-1, 10, 0, & | ||
- | 0, 1000, 0, & | ||
- | | ||
- | !&> | ||
- | </ |
dev/codingconventions.txt · Last modified: 2020/08/21 10:15 by 127.0.0.1