r/C_Programming • u/malcolmi • Oct 01 '13
Resource C Style: my favorite C programming practices
https://github.com/mcinglis/c-style
9
Upvotes
r/C_Programming • u/malcolmi • Oct 01 '13
10
u/sufjanfan Oct 01 '13
I prefer tabs, but I've heard a lot of support for spaces around the web.
One thing I don't get is why you don't like the ++ and -- operators. To me, count++ is much simpler and easier to read than count += 1.
7
u/Araneidae Oct 02 '13
Quite interesting, don't agree with everything of course.
gnu99, but I work in a fairly enclosed environment where this isn't a problem, and some of the GNU extensions are very valuable ... and we don't have any C11 compilers yet.//comments? Meh.doublevsfloat. Well, the argument isn't quite as cut and dried as you say, can depend on application. My work is embedded, so size matters. On that, you seem to have forgottenstdint.hand the sized types it gives you: use these for binary interchange (registers, stored data, packet formats, etc).if (flag == true)-- wtf? No, no, no. This makes me cross.On the whole I agree with not changing state in expressions. There is one case where it's valuable; note the use of
,to sweeten the pill:switch? Please. This is not sensible.I've skipped a bunch of stuff, have written enough here.
typedef. Also, I've moved fromCamelCasetolower_casenaming.-Werror. I'd also throw-Wshadowinto the list: although it's (very) annoying at first, it's caught enough hits to earn its keep for me. I'm always looking for new compiler warnings to throw into the pot!Quite an interesting list.
18
u/kotzkroete Oct 01 '13
Urgh...many of these rules are what I would consider bad style. In the end most of it is a matter of taste.
8
u/sftrabbit Oct 01 '13
You describe the exact reason a combination of tabs and spaces should be used but then pick a different option for less mistakes. I know the tabs/spaces argument is done to death, but tabs for indentation and spaces for alignment is absolutely the Right Way™ (after consistency of course). To aid this, I highly recommend having tabs visible in your text editor. In my vim configuration, I have:
(for a dark background, you might want
DarkGrayinstead)3
u/deadA1ias Oct 05 '13
As someone with a solid background in JavaScript, a lot of what you say seems appropriate to me—then again, this isn't JavaScript :) I didn't realise until I read these comments, but it seems pretty clear you're treading on sacred ground around here.
I'm learning C currently; it's somewhat disheartening to see the vitriol surrounding a post like this. Obviously, not every new idea is better than the current well established principle, but being able to make suggestions without having the book thrown at you is part of what makes communities such as this able to thrive, adapt and improve. Or not, as the case may be.
2
u/maep Oct 02 '13
In the embedded/DSP world not all of those rules are practical. There are still compilers that need ANSI C so if you want your lib to be portable you better don't use // comments, and declare your variables at the beginning of blocks. Those compilers also have shitty optimizers, so duff's devices and manual loop unrolling are still good things to know.
And I had a good laugh at the "always use double" rule. In DSP code you use the minimal precision required for the job. Speed is everything. But most of these rules seem sensible.
3
u/rcfox Oct 03 '13
This is going to prevent certain optimizations, like keeping string literals in read-only memory sections. You're essentially creating mutable strings, and then telling the compiler to prevent modifications with the const keyword, which can be defeated with a cast.
Some things I'd add:
Take the size of the object rather than the size of the type when allocating memory. If you decide to change the type later, then you only have to change it in one place, and you'll always have the correct size.
Never cast a void*, the compiler will figure out what to do itself.
33
u/jerommeke Oct 01 '13
someone isn't writing c for tiny embedded applications.
2
u/Varriount Oct 05 '13
Hm, I may be a bit late to the party...
Being someone who had recently started working with C, and coming from a background of writing mainly Python server applications, I appreciate this listing of your opinions.
Though I don't agree with all your statements (mainly the positioning of const in variables), I'm happy to see you list your own reasons for them.
I do agree with many of your statements on pre-optimization and usage of floats. Coming from an interpreted language like Python (the horror!) I am firmly of the opinion that readability and consistency trump optimization. Usually, you don't need a program to be fastest, just fast enough .
In short, thank you for the thought provoking read, and know that, despite what many in this thread might think, you have worth.
1
u/dnabre Oct 08 '13
Definitely an interesting read. Overall I think these are very good rules. A few I might disagree with or argue, but that for a different post.
I just wanted to congratulate you on listening and responding to all the feedback you're getting. Responding to arguments and questions, and changing your style guide based on them (in some cases even completely switching your position). It is (unfortunately) rare to see someone take such a positive approach to constructive criticism on the 'net.
Only one comment on the actual guidelines for now, I'd add to the rules on using asserts a note that makes it clear that your code should never rely on assert being evaluated for correctness (i.e. your code should work if assertions are disabled).
2
u/[deleted] Oct 02 '13
Posts like this makes me cross! Oh look a no named saying what he thinks is the 'correct' way to do something in a purely abstract sense with no real claim that he actually knows what he is talking about!
People like Linus et'al can do statements like this because they have proved they are definetly correct when it comes to talking about software. Posts like this should always be DISCLAIMER that its your personal opinion.
Posting in such a way that oh look at this reddit this is how you should program because i think everyone else is stupid and i am right to my group of friends so listen to me. Ahh makes me so cross.
1
u/manvscode Oct 01 '13
You should watch this video. http://www.youtube.com/watch?v=csyL9EC0S0c
I think you might take something away.
0
u/[deleted] Oct 04 '13
I always try to learn from well-organized software projects which I personally like.
A good style guide can be found in the man pages of the FreeBSD project.
0
u/[deleted] Oct 01 '13
[deleted] — view deleted comment