Formatting code with astyle

Main development forum.

Formatting code with astyle

Postby kimmov » Wed Nov 12, 2008 3:43 pm

It is a fact we have several different code layout styles in the source tree. There is external code we don't want to even touch. There is heavily modified diffutils code. There is editor (CrystalEditor) code. And then "our" WinMerge code. And even in "our" code there are different layout styles used, even within single files.

I think we should start using code formatting tool to enforce the code layout. There are many tools, but one of the best is Artistic Style.

Running astyle with these options:
Code: Select all
AStyle.exe --style=ansi --force-indent=tab=4 --unpad=paren --max-instatement-indent=8

creates pretty much the style we have used.

DON'T FIX ALL FILES IMMEDIATELY.Even though it would be tempting to start fixing the style for all the files right away, that is the last thing we need. It only creates an enormous amount of noise in version control and makes applying patches harder. But when you modify some file, it would be a good to run astyle and at least fix the code you are modifying.

Making code style more consistent is definitely a good thing. So pure style fixing commits now and then aren't forbidden. Just don't alter every file in SVN with whitespace-changing commits right away.

Other code layouts
Like mentioned above we have other code layouts in our source tree. I think we can use astyle with them too:
  • diffutils: GNU style (--style=gnu)
  • editor: GNU style (--style=gnu)
kimmov
 
Posts: 562
Joined: Thu Sep 11, 2008 8:51 pm
Location: Finland

Re: Formatting code with astyle

Postby kimmov » Thu Jul 23, 2009 4:17 pm

I've got too annoyed to fix style issues in patches. Its not worth of my time and people just don't seem bother doing it themselves. So lets force this style thing now. Short and simple rule for new code is:

The code submitted must be Astyle-formatted.

I've just added runastyle.bat to root of the source tree. This batch file runs Astyle with proper parameters for most folders containing source files. Obviously it causes a lot of changes in current state of the sources. Like I said above, lets not fix all the sources right now. But we definitely need to start fixing sources during other modifications.

If you change a code in file, you might first submit a patch (or commit the change directly with message like "Astyle fixes") that fixes the style. And then a patch that contains the actual change. Both patches can be in same (patch) tracker item. You can fix one (or few) files by copy/pasting options from the batch and instead of giving *.cpp giving the filenames to fix.

If you don't like the formatting Astyle (script) does, you can submit a patch to change the options and convince me it is a good thing.
kimmov
 
Posts: 562
Joined: Thu Sep 11, 2008 8:51 pm
Location: Finland

Re: Formatting code with astyle

Postby kimmov » Thu Jul 23, 2009 4:24 pm

One option is to use Artistic Style for Windows which is a GUI for Windows. Looks like it allows formatting parts of the files too.
kimmov
 
Posts: 562
Joined: Thu Sep 11, 2008 8:51 pm
Location: Finland


Return to Developers

Who is online

Users browsing this forum: No registered users and 2 guests