tags:

views:

2555

answers:

7

I'm working for a company that has strict coding style guidelines but no automatic tool to validate them. I've looked around and the only tools I could find were lint like tools that seem to be aimed at verifying what the code does, and preventing bugs and not at making sure the coding style is correct.

What tool should we use, if at all?

NOTE: I'm looking for something for C code, although something that works for C++ would be good as well.

+7  A: 

You are looking for a 'code beautifier'. Uncrustify's a free one.

You only have to be able to describe your coding style in its configuration file, and it'll make sure every file fits the described style.

Vinko Vrsalovic
+3  A: 

I would argue against using a formal tool here. The best enforcement of coding standards is peer pressure. Peer pressure and mutual respect between team members are key elements of that team building environment....no different that the good natured harassing that is necessary when somebody inadvertently "breaks the build", etc.

Review of newer team members' code is a key part of the learning process and integrating into the team and learning the coding standards, etc. If the coding standard is too complicated or subtle to be picked up in this way, then instead of looking for a tool to validate the standard, you should be lookng for a new coding standard.

Tall Jeff
That's why I suggest instead a beautifier which will get the files in the required style instead of a validator which will do no good at all. You can code in any style you want and before checking in, you run the beautifier which will make sure fits the requirements.
Vinko Vrsalovic
@Vinko - You could do that, and it might be helpful to part time contributors or importing external code, but for full time team members, I really do believe you want them all to be "thinking in coding standard" so that you get the real benefit of having a coding standard in the first place.
Tall Jeff
Can't agree more. Code review and well defince coding standards should do the work.
Ilya
+3  A: 

There are (or were) numerous tools for this. One of the oldest is cb (C Beautifier) that was around in ancient versions of Unix - meaning, in this case, Version 7 Unix:

CB ( 1 ) UNIX Programmer’s Manual CB ( 1 )

NAME
    cb – C program beautifier
SYNOPSIS
    cb
DESCRIPTION
   Cb places a copy of the C program from the standard input on the standard
   output with spacing and indentation that displays the structure of the
   program.
BUGS

GNU indent is a vastly more complex and configurable beastie. There were commercial tools as well. For example, Abraxas Software provides a CodeCheck tool, for example; we used that briefly in the mid-90s, but the changes it wanted us to make were (very necessary, but) too intrusive for management.

Jonathan Leffler
+3  A: 

AStyle does what you want:

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.

There's an AStyle Plugin available for Eclipse.

Eclipse also has a code formatter, but I'm not sure if it works in CDT.

skinp
+1  A: 

The traditional beautifier indent, available on every Unix machine. The version found on some is GNU indent, which can be compiled and installed on every machine. GNU indent can read a set of rules from the file ~/.indent.pro, for instance:

--original --dont-format-first-column-comments --no-blank-lines-after-commas --parameter-indentation 8 --indent-level 8 --line-length 85 --no-space-after-parentheses --no-comment-delimiters-on-blank-lines

So, just running indent before commiting guarantees uniformity of the presentation. If you want to enforce it, define a pre-commit hook in the Version Control System you use, which will run indent and refuse the commit if the committed version differs from what indent produces.

bortzmeyer
+1  A: 

From a similar question: Vera++: "Vera++ can be used to find coding style and conventions violations." There's a free Community Edition and the "all its rules are implemented as (Tcl) scripts that can be modified or added by the user".

Eugen Constantin Dinca
A: 

Please use VIM and enjoy the easiness. You can do anything related to source programming by simple commands. Also you can make the VIM more reliable and powerful by editing .vimrc file. The help in it will help you a lot.

/renjith g

Renjith G