11

I have a pretty new code base written in C++. Already I'm starting to see some bad practices creeping into the project (class file with 1000+ lines of code, functions with a lot of parameters, ...).

I would like to stop on these right away with some automated tools which can hook into the build and check for poor coding practices. What suggestions do you have for such tools? I'm interested in metrics but really more interested in a stylistic sort of lint which would highlight functions with 37 parameters.

stimms
  • 39,499
  • 27
  • 88
  • 144

4 Answers4

6

I'm sorry I can't help you with respect to style, but a great metrics tool which supports C++ and is free: SourceMonitor.

In particular, you will get good info like Cyclomatic Complexity (which I find of more value for bad programming practice than number of parameters), as well as lines of code, percentage of comments, longest function, etc.

Give it a try -- and it is very fast as well.

Peter O.
  • 28,965
  • 14
  • 72
  • 87
torial
  • 12,879
  • 9
  • 58
  • 88
  • 1
    When using this tool, try *Modified Complexity*. I'm quoting the the SourceMonitor help: *The Modified Complexity metric option changes the way the complexity metric is defined for new checkpoints added to a project. When this option is enabled, SourceMonitor computes the complexity metric by adding one to the complexity for each switch statement. The case statements within a switch statement are parsed for other metrics, but the case statements themselves do not contribute to the complexity (as they do when this option is not enabled)*. – Wolf Nov 27 '17 at 15:32
3

As with the others I'm not sure of a tool that will judge style. But CCCC will produce numerous metrics that can help you find the trouble spots. Metrics like cyclomatic complexity will give you quantitative evidence where the problem spots are. The downside is that you will have to incorporate these metrics with a style guide that you adopt or create on your own.

nathan
  • 4,963
  • 4
  • 32
  • 46
  • 1
    Note that by now CCCC is way outdated. The latest activity on their website is from 2002 and only support Microsoft Visual Studio 2003 framework. I also had troubles running it, since it was not that much User friendly. I highly recommand SourceMonitor: http://www.campwoodsw.com/sourcemonitor.html instead, as Torial suggested. – ForceMagic Oct 15 '12 at 03:31
  • 1
    @ForceMagic: I believe CCCC is still a viable choice when not running Windows since SourceMonitor does seem to require Windows. There's also a Jenkins plugin to hook it into a build and to visualize regressions. – Benjamin Bannier Aug 12 '13 at 10:48
1

Metrix++ matches this use case and has got several metrics you are probably interested in. Check it here: http://metrixplusplus.sourceforge.net/

Andrew
  • 1,430
  • 1
  • 14
  • 25
  • 1
    It's usually considered to be good form to make it very explicit when you're recommending your own product. – Crowman Aug 12 '13 at 13:22
  • Thanks. Will know about this and do next time. The answer still matches the question. – Andrew Aug 12 '13 at 21:41
-1

Make sure that you always compile with -Wall compiler option and make it practice that no code is to be checked in if warnings persist.

Find a standard style to follow like this one.

ForceMagic
  • 5,535
  • 11
  • 62
  • 83
ddcruver
  • 900
  • 1
  • 7
  • 12
  • 1
    `-Wall` doesn't automagically warn against the issues OP mentioned (long functions, high cyclomatic complexity). – Benjamin Bannier Aug 12 '13 at 10:51
  • Your *this one* link is broken which makes the whole sentence useless. Maybe you're referencing the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)? – Wolf Nov 27 '17 at 15:40