C coding style

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

Love4Boobies wrote:As for the asterisk thing, the following might look a bit odd:

Code: Select all

int * a, * b, * c;
Yep. No comma operator. ;-)
Every good solution is obvious once you've found it.
drew
Posts: 14
Joined: Thu Jun 09, 2011 2:47 am
Location: Calif.

Re: C coding style

Post by drew »

it drives me absolutely crazy when:
  • curly brackets don't have their own line

Code: Select all

else {

Code: Select all

switch( c ) {
  • spaces are used as indentations
  • things inside parenthesis's are padded with spaces

Code: Select all

if ( a == *b )

Code: Select all

while( ( c = getchar() ) != EOF )
  • spaces are used between function names, statements, and parenthesis's without any consistency
here's a space

Code: Select all

if (
here's no space

Code: Select all

while(
why does sizeof get a space and not malloc?

Code: Select all

struct foo *bar = malloc(sizeof *foo);
size_t int_width = sizeof (int);
that last one was a more of a pet peeve, but I mean... really... what's with all the unnecessary whitespace that doesn't even really make it easier to read? and then not even enough whitespace that makes it MUCH easier to read for the curly brackets? spaces for indentations just makes a giant mess, and takes more space for your source code. it's not much, but with 1k lines of code and indentations everywhere, the space they use adds up. it could easily be an extra several kilobytes... that's nothing really... but doesn't it make you sick knowing that something is being wasted? or is our minds not in the maximum-perfectionist-efficiency state of minds? and if it's not, what are we doing trying to reinvent the wheel? and don't even claim not to be a perfectionist in the least, or you wouldn't even be bothering with a standard for code format. it's kind of a paradox from my point of view or more of an oxymoron.

I'd rather put 10 new lines between each of my functions than have 20 spaces for each indentation I need to make. clearly that's the biggest thing that drives me nuts with the way people format their code. for the love of ASM... stop using your space bar like it's an entire drum set... =P~
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: C coding style

Post by Neolander »

drew wrote:and then not even enough whitespace that makes it MUCH easier to read for the curly brackets?
If you use large enough identations (4 spaces and more), you can clearly separate blocks of code visually, and lines with an alone bracket start to feel like void, at least in my experience. Guess it's a matter of habits and opinion.
spaces for indentations just makes a giant mess
Care to be more precise ? In my experience, it's tabs which result in a mess when you open the source with an editor that has different tab size.
, and takes more space for your source code. it's not much, but with 1k lines of code and indentations everywhere, the space they use adds up. it could easily be an extra several kilobytes... that's nothing really... but doesn't it make you sick knowing that something is being wasted? or is our minds not in the maximum-perfectionist-efficiency state of minds?
Frankly, a few kilobytes are negligible nowadays. A single normally-sized picture takes much more space, yet many people love screenshots (me included).
and if it's not, what are we doing trying to reinvent the wheel? and don't even claim not to be a perfectionist in the least, or you wouldn't even be bothering with a standard for code format. it's kind of a paradox from my point of view or more of an oxymoron.
It's about making it easier to read code. The human mind has the ability to learn and perform things faster when it often encounters the same thing, included code that's formatted in the same way, it's sad not to use this possibility and make every piece of code look different and foreign to the reader.
I'd rather put 10 new lines between each of my functions than have 20 spaces for each indentation I need to make. clearly that's the biggest thing that drives me nuts with the way people format their code. for the love of ASM... stop using your space bar like it's an entire drum set... =P~
You know, most people just set up their text editor so that TAB adds the right amount of spaces and stop to bother.

And although I agree that 20 spaces is too much, 4 or 8 clearly improve readability compared to 2. And force you to reduce conditional structure nesting in your code, which is a Very Good Thing...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

@ Fanael: Code style arguments have literally destroyed more than one otherwise perfectly good programmer team. Believe it or not, code style is important (as in, find a style and adhere to it, not as in, argue about it all day long).

All the arguments pro and con have long since be fielded elsewhere. There are no new arguments that could be fielded. It's all a matter of how much weight you put on which argument.

That's probably why we developers love argueing about it ad infinitum. :P

TAB vs. space...

TAB settings differ between editors. What looks well-aligned in one editor looks bad in another. Worse: Even with tabs, you need to agree on what width those TABs should be represented as on-screen, or you will get into whitespace edit wars.

One person uses 8-space TABs. In a struct, he uses one TAB to indent the type, and one TAB to have the struct members vertically aligned. "Looks good", he says.

Code: Select all

        int     foo;
        double  bar;
The next person, out of personal preference, uses 4-space TABs...

Code: Select all

    int foo;
    double  bar;
It is not immediately obvious that the source is correctly indented if using the correct TAB setting, or what that TAB setting should be.

Using a certain number of spaces solves these problems. Code looks identical regardless the TAB setting, the "correct" indent width is obvious from surrounding code - and it is very easy to make automated checks for violations of the "no TAB" rule.

Besides, we're talking a code style for the Wiki here. Try typing a TAB in the browser. :twisted:
Last edited by Solar on Tue Jun 28, 2011 8:43 am, edited 1 time in total.
Every good solution is obvious once you've found it.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: C coding style

Post by quok »

berkus wrote:Since we stopped talking about the guide, and started talking about personal preferences, here's my take on parens:

There's no space after function names, e.g. main(
But there's space after reserved words, e.g. while (

Makes it easy to grep. Looks nice.
phpBB needs a 'like' or '+1' button. I'd upvote this. :)
Fanael
Member
Member
Posts: 38
Joined: Fri Oct 16, 2009 9:20 am

Re: C coding style

Post by Fanael »

Solar wrote:@ Fanael: Code style arguments have literally destroyed more than one otherwise perfectly good programmer team. Believe it or not, code style is important (as in, find a style and adhere to it, not as in, argue about it all day long).
Believe me or not, I know this. It's just arguing about code style what is pointless and silly.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

Asking for discussion about a style guide probably isn't the smartest move, either...

Well, there you are. I've "been bold" and changed the "Request for Comment" into a Style Guide ("eat it or leave it"). I've used what's usually referred to as "ANSI style", as over the years I've found this meeting the least overall resistance from C/C++ coders. (You'll never get no resistance...) It's also the style I personally prefer, so flame away.

I've also cut away some of the stuff I feld was walking the line between "Wiki style guide" to "project style guide". We'd never see the end of it if we cross that line.

I won't beg Love4Boobie's pardon, and I won't ask for "comments" for the exact reason Fanael just pointed out.

Is there a good reason to not accept this as Wiki Style Guide?
Every good solution is obvious once you've found it.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: C coding style

Post by quok »

Solar wrote: Is there a good reason to not accept this as Wiki Style Guide?
I find this to be very similar to my own preferences and aside from a few small nitpicky things which I won't bring up, I'm perfectly happy with this style guide. I say we just move it out of Love4Boobies' user space and run with it.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: C coding style

Post by Kevin »

Solar, you mean other than looking totally ugly? ;)

But seriously, is it intentional that you have a space after if, but not after while? Usually I would suspect a typo there, but after such coding style discussions you're never certain...
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

Kevin wrote:...is it intentional that you have a space after if, but not after while?
I have no idea what you're talking about. 8)
Every good solution is obvious once you've found it.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: C coding style

Post by Kevin »

Oops, I meant switch, not while. :P
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C coding style

Post by Solar »

Ah. Hah. Hah. :roll: :lol:
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: C coding style

Post by Combuster »

No obvious flaws other than the g++-must-like-it command line that got typoed (and consequently fixed).

If we need a choice, this is probably as good as it gets. I plan on merging this to the manual of style, pending some more time for auditing.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: C coding style

Post by gravaera »

*Cough* 4 spaces can gtfo, tabs are better. If you want 4 space indenting, set your editor to display tabs as 4 space width. You can't do that with raw spaces. Tabs make everyone happy :wink: </troll>

Edit: Tbh though I'm mainly being mischievous because I will probably never submit wholoesale code to the wiki. Most of my contributions have been theory so far, so yea lol :>
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: C coding style

Post by Love4Boobies »

Generally speaking, using hard tabs is problematic because it will misalign your code. Here's an example:

Code: Select all

for (p = list; p != NULL; p = p->next) <TAB> // hello,
<TAB> printf("%p ", p); <TABS> // world!
For the comments to be aligned:
  • <TABS> = 5 <TAB>'s, for hard tabs set to 4 spaces
  • <TABS> = 2 <TAB>'s, for hard tabs set to 8 spaces
Using hard tabs only on the left side isn't any better; when you work with a multi-column alignment, you're screwed again:

Code: Select all

if (a == b) <SPACES1> // hello,
<TAB> c = d; <SPACES2> // world!
In the above, the number of <SPACE>'s in <SPACE2> depends on both <TAB> and <SPACES1>, not just the latter.

The solution is to never use comments on the same line as code, but I see that as a limitation rather than an advantage.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply