Changes between Version 2 and Version 3 of RFC4


Ignore:
Timestamp:
Mar 22, 2017, 5:40:39 PM (7 years ago)
Author:
mloskot
Comment:

First draft

Legend:

Unmodified
Added
Removed
Modified
  • RFC4

    v2 v3  
    33This document proposes and describes desired code formatting style used across C/C++ source code in GEOS.
    44
    5 ||RFC 4:     || Code Formatting Style||
    6 ||Author:       || Mateusz Łoskot ||
    7 ||Contact:      || mateusz@loskot.net ||
    8 ||Status:       || Draft ||
     5||RFC 4:  ||Code Formatting Style||
     6||Author: ||Mateusz Łoskot      ||
     7||Contact:||mateusz@loskot.net  ||
     8||Status: ||Draft                ||
    99
    1010== Summary ==
    1111
    12 This document proposes and describes desired code formatting style used across C/C++ source code in GEOS.
     12The document proposes and describes desired default code formatting style guidelines for GEOS programming in C and C++ languages.
     13
     14The goal of this document is to initiate process to reach an agreement for the default code formatting style.
     15
     16== Motivation ==
     17
     18There is a need to decide on format of GEOS source code and apply such globally consistent format to GEOS C/C++ codebase.
     19
     20A 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
     22The 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
     26The 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.
    1327
    1428== Proposal ==
    1529
    16 The goal of the proposal is to provide guidelines for the default code style when programming in C/C++ for GEOS.
     30It is important to make effortless for developers to produce properly formatted code.
     31
     32The 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
     34The `clang-format` is a tool to automatically format C/C++ code, so that developers don't need to worry about style issues.
     35Unlike other tools which use own parsers, `clang-format` uses the Clang tokenizer and supports the same C++ source code as the Clang compiler.
     36This 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
     38The style settings are defined in `.clang-format` configuration file for our [https://clang.llvm.org/docs/ClangFormatStyleOptions.html style settings].
     39
     40The `clang-format` is straightforward to run and can support development workflow as standalone tool or as one of many editor integrations.
     41
     42Although 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
     47What code formatting rules to use?
     48
     49`clang-format` offers several defaults (eg. LLVM, Mozilla, Linux, Google C++ Style).
     50
     51The 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
     53The 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
     59work-in-progress
     60
     61{{{
     62---
     63BasedOnStyle:   Mozilla
     64Language:       Cpp
     65Standard:       Cpp03
     66ColumnLimit:    80
     67IndentWidth:    4   
     68TabWidth:       4   
     69UseTab:         Never
     70
     71BraceWrapping:
     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
     83BreakBeforeBinaryOperators: None
     84BreakBeforeBraces: Allman
     85BreakBeforeTernaryOperators: 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
     91like indentation, whitespaces and end-of-line markers for distinguished types of plain text files.
     92
     93The `.editorconfig` file will have to be updated to match the chosen `.clang-format` settings.
     94
     95=== Big Reformat ===
     96
     97What to do about the existing code?
     98
     99The proposal recommends to just do one big reformat of the codebase.
     100
     101While 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
     103Partial 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
     107How 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/