Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

It seems to me to be a fairly common thing to do, where you have some code that you'd like to automatically run through a code style tool to catch when people break your coding style guide(s).

Particularly if you're working on code that has multiple languages (which is becoming more common with web-language-x and javascript), you generally want to apply similar code style guides to both and have them enforced.

I've done a bit of research, but I've only been able to find tools to enforce code style guidelines (not necessarily applying the code style, just telling you when you break code style guidelines) for a particular language. It would seem to me a reasonably trivial thing to do by just using current IDE rules for syntax highlighting (so that you don't check style guide rules inside quotes or strings, etc) and a whole lot of regexes to enforce some really generic things.

Examples:

  • if ( rather than if(
  • checking lines with only whitespace

Are there any tools that do this kind of really generic style checking? I'd prefer it to be easily configurable for different languages (because like it or not, some things would just not work cross language) and to add new "rules" to check new things.

share|improve this question

closed as off topic by gnat, Walter, Matthieu, Jim G., Ryathal Sep 18 '12 at 18:37

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

Pretty-Print/diff

Did you try googling "pretty-print" with your language name? Seems like this is doable if the only thing you are changing is the optional whitespace permitted by the language. You can run diff on the output of a comand-line pretty-printer vs. the original code if you want to raise errors or see differences.

Decompile

When I've had to work with the most hideous imaginable Java code where the comments were useless (or nearly so), I've used the JAD Java Decompiler on the .class files and set the options to format everything the way I wanted. Sometimes you can do this and cut and paste useful comments from the original. It's nice because you can tell it not to decompile unused code, so you don't waste time reading stuff that never gets called.

Regular Expressions with grep to raise errors

I have a script that I run to capture common errors that I make. For instance, leaving a form with a get instead of a post:

# Enable extended globbing in scripts so I can use !(validate.sh)
# instead of * to mean not this script
shopt -s extglob

egrep --exclude-dir='.svn' --color -rIi "method[ \t]*=[ \t]*['\"]get['\"]" *

I have a bad habit of using an apostrophe with "its" sometimes when I shouldn't, so this checks for spelling mistakes:

# use "its" for possessive
egrep --exclude-dir='.svn' --color -wrIi "(have|keep|change|be|to) it's" *
egrep --exclude-dir='.svn' --color -wrIi "it's own" !(validate.sh)

# use "it's" for a contraction it-is or it-has
egrep --exclude-dir='.svn' --color -wrIi 'its (not|yours|been|finished|on|ever|slow|probably|an?|never|better|possible)' * 

Really, you can check for just about anything you dream up. Instead of grepping *, you can grep *.html, or *.rb, or whatever for various kinds of issues.

Regular Expressions with sed to fix errors

Use sed to do replacements

# Strip trailing whitespace and DOS line ending trash
sed -i 's/[ \t\r\n]*$//' myProject/myCo/app/pkg/*

# tabify
sed -i -e 's/    /\t/g' *.html

Remember, in sed, you have to escape the plus sign to mean "one or more" in a regular expression, otherwise it matches a literal plus. That throws me every time.

Regular Expression Book

Mastering Regular Expressions, 3rd Edition: Understand Your Data and Be More Productive by Jeffrey E.F. Friedl. One of my all-time favorite tech books. Starts off a little slow, ends up a little fast. I think I made it through this book in 2 weeks with a 2-hour/day train ride and a laptop. Have used RegEx ever since for stuff just like this and a thousand other little things as well.

share|improve this answer
I guess I could just use a pretty printer and compare the original to the pretty printed... If they aren't exactly the same, then something's happened. The only problem I have with that is that it's not really very configurable or extendible. – FuzziBear Sep 12 '12 at 4:24
OK, updated my post with another way to catch or fix errors. Should work on OSX/Linux as a native bash shell. On Windows, run in Cygwin or similar. – GlenPeterson Sep 12 '12 at 4:45
It's not entirely what I was hoping for, but I will give it a couple of days. If nothing comes along, I will accept this answer :) – FuzziBear Sep 12 '12 at 23:28

TAke a look at http://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier

I would consider Uncrustify as my first suggestion.

share|improve this answer

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