From my own projects, I have noticed a trend – code is never consistently styled. I change my own coding style every once in a while, so over the years I notice a siginificant change in source code – even within the same project! I am of the opinion that syntax is not that important, but it is offputting when you see different conventions, and you want to make them consistent. I ended up spending a lot of time restyling old code to match my new conventions – time I should be spending writing games.
Recently I did a bit of programming in go and I liked their approach to this problem. Go provides a tool called gofmt which automatically styles source code to the de facto go code style. This means all go programs ending up looking fairly consistent.
This looked pretty cool to me, but I do most of my programming in C/C#/python. There is a standard for styling python programs called pep8 which solves this problem nicely, and pylint will help keep you close to the standard.
For C and C#, there does not seem to be any canonical formatting style (Microsoft do give some guidelines on style for C#, but I don’t know of any free tool which enforces these – please comment if you do).The wikipedia page on indentation style alone gives many options for arranging curly braces, all with their own advantages and disadvantages. None of them match with what I consider good style either.
In the absence of a standard, we can come up with our own. As long as we are consistent with the standard, there is no problem. So we need a tool which will style our code to this specification. I found 2 free tools which do this :
Both of them are good tools, but astyle had more options I cared about and supports both C and C#, so I went with that. After half an hour of experimenting with options I eventually came to this line to format a C file:
|
1 2 3 4 |
astyle --style=java --suffix=.astyleorig --unpad-paren \ --delete-empty-lines --add-brackets --convert-tabs \ --align-pointer=type --lineend=linux --suffix=none \ --indent-switches main.c |
Now I put this into a bash script called ”enforce_coding_style.sh” in the root of my repo to style all of our source files:
|
1 2 3 4 5 6 7 |
#!/bin/bash find . \( -name '*.c' -o -name '*.h' \) -exec \ printf "Indenting file {}\n" \; -exec \ astyle --style=java --suffix=.astyleorig \ --unpad-paren --delete-empty-lines --add-brackets \ --convert-tabs --align-pointer=type --lineend=linux \ --suffix=none --indent-switches {} \; |
This will also force unix line endings which is nice. So that we never have to worry about the problem again, we can call this script from a git pre-commit hook in our repo “.git/hooks/pre-commit”:
|
1 |
bash ./enforce_coding_style.sh |
Make sure everyone who uses the project has this enabled, and your code will look consistent forever more. If you ever want to update the standard, you just need to update the options passed to astyle in the bash script.
Update:
Improved bash script thanks to feedback from claudius on hacker news.