For a while I've been maintaining an ad-hoc script to check code style against our project's guidelines.

I'm aware of AStyle/Uncrustify and have used them, but they are best for once-off code-style cleanups, but impractical to just run all code through some automatic process after every commit.

To explain the issue a little more, yes - we have code-style documentation, but devs don't always follow exactly, and it's not really great to give them personal mail every time they make some tiny change that goes against some convention.

So I took the approach to deal with this like compiler warnings, report inconsistencies but leave it up to the developer to resolve (some of you may be familiar with Python's pep8 style checker). Once every so often we do style cleanups.

In general I'm very happy with the outcome (unlike most style checking tools), you get to get a lot of flexibility - so you can choose where to be strict and where to be relaxed about conventions (avoid too many false positives is important too).

Currently I'm using Python and Pygments to parse C source code (some C++ also but mainly C) and then my own script to check the tokenized results meet our conventions. This works fairly well, I don't need the full AST you can get with GCC or Clang for example, simply knowing if text is a comment, string, punctuation, preprocessor... etc. is enough (what you would get from syntax highlighting, or a source tokenizer).

But I did end up having to write quite a few utility functions for the source:

  • extract the body of a block between brackets.
  • find matching brackets.
  • identify casts.
  • inspecting whitespace around punctuation.

... nothing really advanced.

The exact conventions are probably not so important, but for an example here are some checks:

  • Placement of braces for 'if' statement checks broken into multiple lines.
  • Placement of break in a switch statement.
  • Spaces around operators, braces, function calls, after commas.
  • Indentation for multi-line if statements.
  • Indentation consistency.

At this point I would be interested to move away from my own utility functions but still have a script which checks code-style, if there is something else well maintained solving similar problems.

I'd be surprised if there is not some other people doing similar projects to maintain consistent source style in this way, so I'm interested to know of any alternatives available.


Considering the workflow I have described, how can projects successfully maintain code-style?

  • 2
    Good point, but what's your question? – Arseni Mourzenko Aug 19 '13 at 7:52
  • @MainMa, made question more clear. – ideasman42 Aug 19 '13 at 10:04

Depending on the languages, static analysis tools may exist or not. For example, for C#, there is StyleCop and its automatic integration within Visual Studio, letting to check the style errors in one click (actually two clicks, but there is a keyboard shortcut too). Using third-party tools, it can also be integrated into many version control systems, meaning that only compliant code may be committed.

For C and C++, see A free tool to check C/C++ source code against a set of coding standards? I haven't used it, so I don't know how well it is and whether it is possible to integrate it into a version control.

In all cases, version control integration is a must have, for two reasons:

  1. If there is no such integration, there is no enough pressure on the developers to respect the style rules. Doing it during code review is a waste of time: code reviews are not for that. Finally, constantly reminding to the developers to check their style will inevitably decrease the quality of your relations with your colleagues.

  2. Because one can commit for a few days without being recalled to check the style, it's much harder to enforce hundred rules after ten days of work than ten rules every day. The process should be as frequent as possible: the best technique would be to move the burden of style rules to the IDE, but actually, IDEs are quite shy when it comes to enforcing a style (including when they asked to do it, for example through the auto-formatting feature).

  • I dont think static analysis tools tools really help here, Coverity and Cppcheck for example find style issues that may relate to invalid code (bad indentation or odd assignments), but they are not style checkers by any means and lack many checks you would want for this purpose. What gets conflated here is code style vs valid-code, lint/clang-static-checker/cppcheck etc... are not helpful here. you really want a tool that allows to specify rules, then check the code against those rules. – ideasman42 Aug 19 '13 at 13:07
  • Have you looked at c++lint from the answer I linked to? – Arseni Mourzenko Aug 19 '13 at 13:16
  • c++lint seems to be something along the lines of what I already have since it uses its own hard-coded rules (from reading the page). So only really useful if your project already follows googles style guide. – ideasman42 Aug 20 '13 at 4:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.