Coding-conventions: astyle

Hey everybody,

I created a astylerc for automated coding style fixing. It should respect the given rules from: https://github.com/RIOT-OS/RIOT/wiki/Coding-conventions

[RIOT] $ cat .astylerc --style=linux --brackets=attach --indent=spaces=4 --indent-switches --indent-labels --indent-col1-comments --min-conditional-indent=0 --break-blocks --pad-oper --unpad-paren --break-closing-brackets --add-brackets --align-pointer=name

After running this we have a lot changes: 209 files changed, 15148 insertions(+), 12980 deletions(-)

but the code is more readable and maintainable....

If you want to try it: just run the following: [RIOT] $ find -name '*.c' -or -name '*.h'| xargs astyle

I can also create a pre commit hook to ensure this style!

So what do you guys think?

Christian

Hi Christian!

I created a astylerc for automated coding style fixing.

Thanks for your effort!

After running this we have a lot changes: 209 files changed, 15148 insertions(+), 12980 deletions(-)

but the code is more readable and maintainable....

That's the general idea behind the coding conventions. :wink: However, in some (rare) cases it might be beneficial not to stick too the coding conventions too literal.

For example core/bitarithm.c

The current code is:     r = (v > 0xFFFF) << 4; v >>= r;     shift = (v > 0xFF ) << 3; v >>= shift; r |= shift;     shift = (v > 0xF ) << 2; v >>= shift; r |= shift;     shift = (v > 0x3 ) << 1; v >>= shift; r |= shift;                                             r |= (v >> 1);

astyle would convert it into:     r = (v > 0xFFFF) << 4;     v >>= r;     shift = (v > 0xFF) << 3;     v >>= shift;     r |= shift;     shift = (v > 0xF) << 2;     v >>= shift;     r |= shift;     shift = (v > 0x3) << 1;     v >>= shift;     r |= shift;     r |= (v >> 1);

In this case I would definitely prefer the non-cc (aka the current) version.

I can also create a pre commit hook to ensure this style!

In fact, a nice idea. I'm not an expert on git commit hooks. Is it possible to configure these hooks in an optional manner? Or is there any way to declare exceptions from the coding conventions for particular pieces within the code?

Anyway, I will browse through the entire suggestions of astyle and probably apply most of the changes to the repository.

Cheers, Oleg

Since hooks are simply scripts there could be a y/n question before your commit.

Regards, Martin

For example core/bitarithm.c

The current code is:      r = (v > 0xFFFF) << 4; v >>= r;      shift = (v > 0xFF ) << 3; v >>= shift; r |= shift;      shift = (v > 0xF ) << 2; v >>= shift; r |= shift;      shift = (v > 0x3 ) << 1; v >>= shift; r |= shift;                                              r |= (v >> 1);

astyle would convert it into:      r = (v > 0xFFFF) << 4;      v >>= r;      shift = (v > 0xFF) << 3;      v >>= shift;      r |= shift;      shift = (v > 0xF) << 2;      v >>= shift;      r |= shift;      shift = (v > 0x3) << 1;      v >>= shift;      r |= shift;      r |= (v >> 1);

In this case I would definitely prefer the non-cc (aka the current) version.

your example above isn't the default case, this is hand tuned bit operations... 99% of the code's styling is "just wrong" in terms of the style guide.

I can also create a pre commit hook to ensure this style!

In fact, a nice idea. I'm not an expert on git commit hooks. Is it possible to configure these hooks in an optional manner? Or is there any way to declare exceptions from the coding conventions for particular pieces within the code?

ah, I don't think so, not with astyle.

Anyway, I will browse through the entire suggestions of astyle and probably apply most of the changes to the repository.

nice, most of the changes are very useful.

Hi!

Anyway, I will browse through the entire suggestions of astyle and probably apply most of the changes to the repository.

I've just pushed the corresponding changes to RIOT's master branch: 207 changed files with 16,614 additions and 13,516 deletions.

Cheers, Oleg

Hi,

Oleg Hahm schrieb:

Anyway, I will browse through the entire suggestions of astyle and probably apply most of the changes to the repository.

I've just pushed the corresponding changes to RIOT's master branch: 207 changed files with 16,614 additions and 13,516 deletions.

It appears the astyle does not adhere to our conventions when it comes to spacing if-statements (if (foo) vs. if(foo)).

Cheers, Ludwig

Hi!

It appears the astyle does not adhere to our conventions when it comes to spacing if-statements (if (foo) vs. if(foo)).

Hmm, too bad. I wasn't careful enough here. However, though Linux' coding style proposes to use a space after keywords such as if, switch, case, for, or while, I personally don't have any strong feelings for the one or the other option. What about you? Would you agree to leave the code as it is (i.e. without a space between these keywords and the opening parenthesis) and just update the coding conventions? Or is there any strong reason to avoid this?

Cheers, Oleg

I would absolutely prefer the space between a keyword and the parenthesis. This convention is much more common and more readable.

Cheers, Heiko

I haven't followed the discussion very closely, but this is indeed the expected way to write C.

  foo(bar);

but

  if (bar)     ;

... and One True Brace Style (http://en.wikipedia.org/wiki/Indent_style), Kernel style (https://www.kernel.org/doc/Documentation/CodingStyle), but never use HT characters ("Tabs", http://www.jwz.org/doc/tabs-vs-spaces.html), ... there are enough sources on this, so I'll stop here.

Grüße, Carsten

Dear rioters,

It appears the astyle does not adhere to our conventions when it comes to spacing if-statements (if (foo) vs. if(foo)).

okay, let's obey the Linux coding conventions then. I fixed it in c8bee9e554f58da3a74c7a40374064a1fbf9890f. However, I didn't figure out how to tell astyle to do this. (In fact, sed was my friend.)

Cheers, Oleg