views:

214

answers:

6

I'm looking for a script/tool that can be customized to check and enforce coding/naming conventions on a C/C++ code.

It should check for example:

  • Code lines are wrapped at some length.
  • Private variables have prefix _
  • Code is indented properly.
  • All functions are documented.

Many of the projects I'm working on are outsourced by customers which tend to have various internal coding and naming conventions.

+2  A: 

The GNU indent tool can do some of what you're asking for. Not sure if it can check for documentation, but the rest sounds doable

Sam Post
I like GNU indent, but it does not test coding and naming conventions.
Eddy Pronk
+2  A: 

Don't know if it's really worth the time, but if you really want, you could try building something similar to google's cpplint, which does checking of their style guide.

I personally think that thorough reviews and people's commitment to follow certain conventions is a much better way to make sure your code is "indented properly" and variables named consistently. Maybe invest in making a document like google has done, describing the details of acceptable style, and have people in your company accept it and stick to it.

Why humans are better than a script? The answer is quite simple: people will have to deal with the code later, they should care about the stuff they are writing. A variable name like i will be fine for a script, but not going to slip from my eyes, if it is a clientCount, it should just be named appropriately, script which would be able to do that might take over the world rather soon. :)

Dmitry
+2  A: 

Universal Indent wraps a whole lot of indention tools in a nice GUI. If anything suits you it is likely included. (Here is the feature list) All major OSes are supported.

There are also other tools checking for other things:

lint for security and misc other things.

For Qt programs you can use Krazy. It's a bit of a hassle to find, download and get it going, since it is hidden in the depths of the KDE source tree (quality assurance sub directories... somewhere). But once working it is really cool. It checks all kinds of cute stuff. Like unused Qt headers. Bad string utilization. Missing tr() statesments for user visible strings, weird iteration constructs, dot dot dot.

What goes without saying is that all tools mentioned are customizable.

Ronny
+1  A: 

Basically, what other people have said. But can I just point out that:

  • All functions are documented.

cannot really be checked by a tool - it requires human eyes and understanding. I suspect we've all seen stuff like this:

//-------------------------------------------------------------
// Name: foo
// Purpose: 
// Returns: integer
//-------------------------------------------------------------

In other words, boilerplate "documentation" that tells you less than nothing. This sort of thing can only be eradicated by code reviews.

anon
A: 

If you have a lot of money, you should check out Klocwork. It has the ability to check source code for following style guidelines. I've used it at different shops. A lot of work to set up and maintain, but very powerful.

Thomas Matthews
A: 

I use Understand 4 c++ made by scitools. It comes with a perl and C api. However I wrote a managed API for it. This tool allows you to scan your source code and analyze it like reflection does for native code. It works really nicely in allowing to you get the names of your variables, classes etc... I personally have written dozens of static analysis tools at work using this API. It would be an easy matter to enforce all kinds of coding standards using this tool. Especially the variable name standard.

The link the for the product: http://www.scitools.com/ and my managed wrapper: http://understandapi.codeplex.com/

C Johnson