12 | | This document proposes and describes desired code formatting style used across C/C++ source code in GEOS. |
| 12 | The document proposes and describes desired default code formatting style guidelines for GEOS programming in C and C++ languages. |
| 13 | |
| 14 | The goal of this document is to initiate process to reach an agreement for the default code formatting style. |
| 15 | |
| 16 | == Motivation == |
| 17 | |
| 18 | There is a need to decide on format of GEOS source code and apply such globally consistent format to GEOS C/C++ codebase. |
| 19 | |
| 20 | A uniform, codebase-wide formatting style makes reading and comprehending existing code easier, writing code focused on important aspects of new developments and more pleasent, removes burden during a patch or pull request code reviews and prevents [http://wiki.c2.com/?WhereDoTheBracesGo bikeshedding religious arguments]. Even in small projects, contributing developers discover the problems of working without an agreed upon code format. |
| 21 | |
| 22 | The utility of such guidelines has been proven by many open source software projects. |
| 23 | |
| 24 | "A mature engineers know that a standard is more important than which standard." ~[MongoDB] |
| 25 | |
| 26 | The scope of the proposal is specifically limited to formatting style guidelines. It is not an intention to develop a general coding guide covering other aspects of writing software like naming, etc. |
16 | | The goal of the proposal is to provide guidelines for the default code style when programming in C/C++ for GEOS. |
| 30 | It is important to make effortless for developers to produce properly formatted code. |
| 31 | |
| 32 | The proposal suggests to use [https://clang.llvm.org/docs/ClangFormat.html clang-format] version 3.8 or higher to define C++ code formatting rules for GEOS code. |
| 33 | |
| 34 | The `clang-format` is a tool to automatically format C/C++ code, so that developers don't need to worry about style issues. |
| 35 | Unlike other tools which use own parsers, `clang-format` uses the Clang tokenizer and supports the same C++ source code as the Clang compiler. |
| 36 | This guarantees correct output is produced and offers unique features (eg. wrapping long lines whether of code, strings, arrays - something which AStyle has no way of doing). |
| 37 | |
| 38 | The style settings are defined in `.clang-format` configuration file for our [https://clang.llvm.org/docs/ClangFormatStyleOptions.html style settings]. |
| 39 | |
| 40 | The `clang-format` is straightforward to run and can support development workflow as standalone tool or as one of many editor integrations. |
| 41 | |
| 42 | Although no means to enforce the default formatting style are proposed, currently used CI services (eg. Travis CI) may be employed as a post-commit safety valve |
| 43 | - a clang-format lint failure as a compile break (eg. [https://github.com/mongodb/mongo/blob/master/buildscripts/clang_format.py clang_format.py] build script used by MongoDB). |
| 44 | |
| 45 | == Code Formatting Rules == |
| 46 | |
| 47 | What code formatting rules to use? |
| 48 | |
| 49 | `clang-format` offers several defaults (eg. LLVM, Mozilla, Linux, Google C++ Style). |
| 50 | |
| 51 | The proposal recommends to use one of the base styles, if necessary, fine-tunning as an easier way to get started than deciding on each option one by one. |
| 52 | |
| 53 | The reasons are two-fold: |
| 54 | * make GEOS code unified with the wide spectrum of well-established C/C++ projects |
| 55 | * long arguments and religious wars prevention. |
| 56 | |
| 57 | === `.clang-format` === |
| 58 | |
| 59 | work-in-progress |
| 60 | |
| 61 | {{{ |
| 62 | --- |
| 63 | BasedOnStyle: Mozilla |
| 64 | Language: Cpp |
| 65 | Standard: Cpp03 |
| 66 | ColumnLimit: 80 |
| 67 | IndentWidth: 4 |
| 68 | TabWidth: 4 |
| 69 | UseTab: Never |
| 70 | |
| 71 | BraceWrapping: |
| 72 | AfterClass: true |
| 73 | AfterControlStatement: true |
| 74 | AfterEnum: true |
| 75 | AfterFunction: true |
| 76 | AfterNamespace: true |
| 77 | AfterObjCDeclaration: true |
| 78 | AfterStruct: true |
| 79 | AfterUnion: true |
| 80 | BeforeCatch: true |
| 81 | BeforeElse: true |
| 82 | IndentBraces: false |
| 83 | BreakBeforeBinaryOperators: None |
| 84 | BreakBeforeBraces: Allman |
| 85 | BreakBeforeTernaryOperators: true |
| 86 | }}} |
| 87 | |
| 88 | === `.editorconfig` === |
| 89 | |
| 90 | [http://editorconfig.org/ EditorConfig] is currently in use and `.editorconfig` file is provied to automatically tell popular code editors about the basic style settings |
| 91 | like indentation, whitespaces and end-of-line markers for distinguished types of plain text files. |
| 92 | |
| 93 | The `.editorconfig` file will have to be updated to match the chosen `.clang-format` settings. |
| 94 | |
| 95 | === Big Reformat === |
| 96 | |
| 97 | What to do about the existing code? |
| 98 | |
| 99 | The proposal recommends to just do one big reformat of the codebase. |
| 100 | |
| 101 | While it may seem causing clutter in the repository log (eg. `svn blame`), if it occurs infrequently (eg. yearly) and is applied to the entire codebase at that time, it should not be very disruptive to the source code history. |
| 102 | |
| 103 | Partial application of the code formatting rules would create more work without delivering the full benefit [MongoDB] leading to codebase with different styles mixed. |
| 104 | |
| 105 | == Best Practice == |
| 106 | |
| 107 | How to work against the natural entropy in a codebase: |
| 108 | |
| 109 | * It is highly recommended to use `clang-format` integration while writing a code. |
| 110 | * Format changed code before committing or opening pull requests. |
| 111 | * If you have to commit change in code formatting, do it in separate commit. Avoid commits with a mixture of code and formatting changes. |
| 112 | ** There is downside of history clutter in repository, but this proposal states that a codebase with different styles across is even worse. |
| 113 | |
| 114 | == Conclusion == |
| 115 | |
| 116 | "After all, every moment of time wasted on code formatting or discussion thereof is eliminated." ~[MongoDB] |
| 117 | |
| 118 | == References == |
| 119 | |
| 120 | * [MongoDB] Succeeding With !ClangFormat: [https://engineering.mongodb.com/post/succeeding-with-clangformat-part-1-pitfalls-and-planning/ Part 1], [https://engineering.mongodb.com/post/succeeding-with-clangformat-part-2-the-big-reformat/ Part 2], [https://engineering.mongodb.com/post/succeeding-with-clangformat-part-3-persisting-the-change/ Part 3] |
| 121 | * https://clangformat.com - `clang-format` interactive guide and builder |
| 122 | * https://zed0.co.uk/clang-format-configurator/ |