1

I just started using Astyle with VS-2013 for C++. I want to configure it to follow the Google C++ style guide. I noticed that Astyle allows configuration files to be imported, so I was wondering has somebody done the hard work and configured it to follow the Google C++ style guide? If not, then what adjustments should I make in the Settings window:

enter image description here

Any ideas?

ahmadh
  • 1,582
  • 2
  • 18
  • 28
  • 7
    It’s worth noting that the Google C++ style guide is heavily criticised by the C++ community and even its supporters argue that it’s highly specialised to Google’s requirements and a bad template for general use. – Konrad Rudolph May 15 '14 at 17:01
  • Thanks @KonradRudolph , that's interesting ... can you point me to some source that points out the problems? Moreover, what style-guide would you recommend following? – ahmadh May 15 '14 at 17:03
  • [Hacker News](https://news.ycombinator.com/item?id=7564529) has a discussion of its shortcomings. The [C++ chat](http://chat.stackoverflow.com/rooms/10/conversation/why-google-style-guide-sucks-roughly) has a few more. Off the top of the head I don’t know a better guide but [Programmers.SE](http://programmers.stackexchange.com/q/223313/2366) already has a question about that. – Konrad Rudolph May 15 '14 at 17:09
  • @ahmadh: Many coders look poorly upon rigid style guides if they have to comply manually. Personally, and since you already mentioned `astyle`, I have had good experiences with setting up an *automatic* Astyle run (with pre-configured options) as part of the unit testing, saving any reformatted files as `.reformatted`. Existence of such files makes the test fail. This way, coders get 1) feedback as to what style is expeced, 2) a convenient way to *apply* the style (by copying the .reformatted file over theirs after checking it), and 3) you don't have to check compliance yourself. – DevSolar May 22 '14 at 10:10
  • (ctd.) If you excuse the bit of self-promotion, check [JAWS](http://jaws.rootdirectory.de) for an example setup for this (in tools/check.cpp.in). (JAWS also checks for character set, both in the filenames and the source file itself.) – DevSolar May 22 '14 at 10:11
  • Couple of comments: (1) I settled for this Astyle: `--style=linux --attach-inlines --convert-tabs --mode=c` (2) I came across this nice utility to quickly try out new code styles, call [UniveralIndentGUI](http://universalindent.sourceforge.net/). – ahmadh May 30 '14 at 18:31

1 Answers1

1

In the first place, I don't think AStyle is powerful enough to let your code totally follow the google C++ style guide. For example, the following code segment, which is an example in Function Calls, cannot be obtained by AStyle, but by manual alignment.

DoSomething(
    argument1,  // 4 space indent
    argument2,
    argument3,
    argument4);

Besides, I don't think the commend line argument --style=google / -A14 aligns well with the style guide.
My own option file is as follow:

# 2 space indent
-s2
# Indent 'class' and 'struct' access modifiers, 'public:', 'protected:' and 'private:', one half indent.
-xG
# Indent 'switch' blocks so that the 'case X:' statements are indented in the switch block. The entire case block is indented.
-S
# Do not retain a backup of the original file. The original file is purged after it is formatted.
#-n
# Don't break one-line blocks.
-O
# Don't break complex statements and multiple statements residing on a single line.
-o
# Attach a pointer or reference operator (*, &, or ^) to the variable name (right).
-k3
# Insert space padding after paren headers only (e.g. 'if', 'for', 'while'...).
-H
# Insert space padding around operators.
-p

IMHO, AStyle will help convert your code in large, but some details can only be converted manually.

lzl124631x
  • 4,485
  • 2
  • 30
  • 49
  • I believe AStyle on the contrary gives perfect argument intendation, looking exactly like you did. by putting tabs until the beginning of the line indent, and spaces from there to the argument beginning. Which is the best next-line-auto-indent I've ever seen in a tool. (emacs->tabify don't do it right, it uses tabs+minimal amount of spaces, like visual studio does.) You must use `--max-instatement-indent=70` to obtain what you (and I) need :) – v.oddou Jun 05 '14 at 05:54
  • @v.oddou. Sorry. I changed the example. I don't think AStyle have a command line argument for the example. Does it? – lzl124631x Jun 05 '14 at 07:05
  • AStyle isn't perfect and that is why I've stopped using it. manual indentation doesn't cost so much and allows super variety. I like AStyle a lot but if it is not bound to ctrl+s I ended up almost not using it, to avoid to mess special places that I pretty-ified by hand. – v.oddou Jun 06 '14 at 00:30