# Code style discussion

**URL:** <https://discourse.itk.org/t/code-style-discussion/930>\
**Category:** Engineering\
**Created:** [May 14, 2018, 4:31pm UTC](https://discourse.itk.org/t/code-style-discussion/930 "2018-05-14T16:31:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [May 14, 2018, 4:31pm UTC](https://discourse.itk.org/t/code-style-discussion/930/1 "2018-05-14T16:31:10Z")

</div>

Maintaining coding style is hard without good tool support. [EditorConfig](http://editorconfig.org/) seems to be the best way to accomplish that over a wide variety of platforms. Are there any volunteers to try to implement ITK’s [style guide](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch13.html#x57-259000C) in an `.editorconfig` file? It is totally OK if this is done incrementally - somebody does a first pass implementing a few rules, which gets expaned by someone else etc.

A possible starting point would be translating ITK’s [uncrustify](http://uncrustify.sourceforge.net/) configuration files. [uncrustify\_itk.cfg](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/Maintenance/uncrustify_itk.cfg) is meant to not overly modify the user-written code, even if it means not fully enforcing the ITK style. [uncrustify\_itk\_aggressive.cfg](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/Maintenance/uncrustify_itk_aggressive.cfg) tries to enforce as many rules as possible, with a side-effect of creating more un-compilable modifications.

Also, as we are doing a major overhaul of the library right now, it might be worthwhile considering a change in code style. We could relax the rules somewhat, or turn some rules into recommendations (e.g. space around template parameters `FilterType<ImageType>` vs `FilterType< ImageType >`). We could also completely rework the rules to make them easier to be automatically and reliably enforced (by e.g. [clang-format](https://clang.llvm.org/docs/ClangFormat.html)). A more standard style (such as [Allman](https://en.wikipedia.org/wiki/Indentation_style#Allman_style)) would be easier to enforce than a completely custom style (currently effective ITK style).

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [May 14, 2018, 6:00pm UTC](https://discourse.itk.org/t/code-style-discussion/930/2 "2018-05-14T18:00:07Z")

</div>

I just found out that EditorConfig does not have many options. I created a [patch](http://review.source.kitware.com/#/c/23431/) to add it. Visual Studio has to be restarted (closing and opening the project is not enough) for “format selection (Ctrl-K, Ctrl-F)” and “format document (Ctrl-K, Ctrl-D)” commands to start behaving differently.

---

<div class="post-metadata">

**Author:** ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)\
**Post date:** [May 15, 2018, 5:58am UTC](https://discourse.itk.org/t/code-style-discussion/930/3 "2018-05-15T05:58:03Z")

</div>

Thanks for looking into this @dzenanz.

I’m ready to help.

Whatever style or set of rules we agree on would be fine as long as we stick to it. Automating the enforcement of the style would help. The [ITK Software Guide](https://itk.org/ItkSoftwareGuide.pdf) (Appendix 3) already covers it extensively. If there is a tool that is able to enforce, show warnings for all aspects, much like PEP8/Flake8 for Python, then we should find it. If there is no such a tool, we’ll need to take care ourselves to stick to the set of rules as best we can, whether they are in one form or another (whether we adopt `FilterType<ImageType>` against the current `FilterType< ImageType >`).

`EditorConfig` seems a reasonable choice. I don’t know of any other.

Some other thoughts:

- Automatically adopting the proposed `.editorconfig` for ITK for the configured IDE would be required.

- I ignore whether/which IDE’s are able to use different `.editorconfig` for different projects. If a tool/way to allow this for those developing projects with different style conventions could be found, the adoption of ITK’s potential `.editorconfig` could be greater.

---

<div class="post-metadata">

**Author:** ![imikejackson](https://discourse.itk.org/user_avatar/discourse.itk.org/imikejackson/32/35_2.png) [@imikejackson](https://discourse.itk.org/u/imikejackson)\
**Post date:** [May 15, 2018, 5:47pm UTC](https://discourse.itk.org/t/code-style-discussion/930/4 "2018-05-15T17:47:33Z")

</div>

My vote would be for Clang-format which is available on the major platforms and integrates perfectly with git via git-clang-format. All you need is a .clangformat at the top level of the project. Visual Studio, Visual Studio Code, Qt Creator, Emacs and Vi all can support clang-format in one fashion or another. This also seems to be the way whole host of other projects are heading.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [May 15, 2018, 6:53pm UTC](https://discourse.itk.org/t/code-style-discussion/930/5 "2018-05-15T18:53:27Z")

</div>

+1 for a clang-format config file close to ITK standards too!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [May 15, 2018, 7:28pm UTC](https://discourse.itk.org/t/code-style-discussion/930/6 "2018-05-15T19:28:04Z")

</div>

`.clang-format` config file [already exists](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/Maintenance/.clang-format). Perhaps it needs to be moved to the repository root for compliant tools to pick it up automatically?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [May 18, 2018, 1:45pm UTC](https://discourse.itk.org/t/code-style-discussion/930/7 "2018-05-18T13:45:15Z")

</div>

Patch [submitted](http://review.source.kitware.com/#/c/23442/).
