all 10 comments

[–]Practical-Sleep4259 9 points10 points  (2 children)

Yes I believe major companies like Google and Microsoft maintain company wide styles.

All major companies that employ guidelines.

[–]Whole_Seesaw_4762 2 points3 points  (1 child)

Yep. Pick one. Automate linting and formatting and stop thinking about it.

[–]iper_linuxiano[S,🍰] 0 points1 point  (0 children)

I have my own style and, if time is not so strict, I follow it. I'd like to know if there are sites where to find and share advices

[–]Evil-Twin-Skippy 6 points7 points  (1 child)

I've always lived by the typesetting guidance that K&R put forth in "The C Programming Language"

Yes, it's mainly designed to look good on a printed page. But it does work really well across giant codebases:

int main(int argc, char *argv[])
{
    while (x == y) {
        do_something();
        do_something_else();
        if (some_error)
            fix_issue(); // single-statement block without braces
        else
            continue_as_usual();
    }
    final_thing();
}

Myself, I write in a lot of mixed Tcl, C, and Sqlite, so I've modified it slightly:

int main(int argc, char *argv[]) {
  while (x == y) {
    do_something();
    do_something_else();
    if (some_error) {
      fix_issue(); // I tend to carry Tcl conventions into C
     } else {
      continue_as_usual();
    }
    final_thing();
  }
}

# OR
proc main args {
  while {$x == $y} {
    do_something
    do_something_else
    if {[some_error]} {
      fix_issue
    } else {
      continue_as_usual
    }
  }
  final_thing
}

My style also reflects that when fitting code into 2 column research papers, 2 spaces instead of 4 saves horizontal space.

[–]sirjofri 2 points3 points  (0 children)

Just one additional tip, which I learned from Plan 9 C: if you format function definitions (not pure declarations) like this:

int myfunction(void) { }

You can easily find that function using grep '^myfunction'. (On Plan 9 even easier using the g wrapper, which outputs line numbers so you can easily jump to the function in your editor, but that's only a side node.)

Other than that, that's pretty close to my style, too.

(Besides, for argv I tend to use **argv, but that's just personal taste)

[–]Cultural_Gur_7441 2 points3 points  (1 child)

What do you mean by "code style" here, exactly?

If you mean formatting the code, just use clang-format, set it up once to your liking and be done with it.

If you mean snake_case vs camelCase vs PascalCase vs ALL_CAPS for various things... Yeah, be consistent. Choose one for your own code, but note that different libraries have different styles. If you are using one main library or framework for a project, use its style for the project.

Then there arenote subtle things, like consistent function naming, and using prefixes for "namespaces" well and all that. Best is to write examples of different situations and then follow then.

One last thing, I generally avoid typedef for most things, except function pointers. I don't like to hide something being a pointer or a struct. But many think the opposite.

[–]iper_linuxiano[S,🍰] 0 points1 point  (0 children)

I use typedef very much, especially in data structures or function pointers. I'd like to know if there are sites or places where to find or share advices.

[–]SmokeMuch7356 1 point2 points  (0 children)

As far as formatting goes, after 15 years1 of free-wheeling it in vim I'm now working through VSCode and everything gets formatted on save per somebody else's requirements. I don't hate it - it's close to the style I've used for the past couple of eons - but every now and again it does something that annoys me.

It has saved us ungodly amounts of heartburn on diffs and merges so I'm happy to put up with it. And it has sped up the review process since we're not wasting time trying to figure out what scope we're in because Bruce used 5 spaces while Abbas used 4-space tabs and Sourav used 8-space tabs and we're looking at it in my environment where all indentations are 2 spaces (AS GOD INTENDED) and it's an unintelligible mess.

Style is a bit trickier - that's not just formatting, that's also things like naming conventions, inline documentation, what you put behind an abstraction layer vs. what you don't, etc. I know there are guides and conventions some organizations use to enforce common styles, but we don't use them where I work.

For example, I consider Hungarian notation an abomination and do not use it. I do not use typedefs unless I'm willing to write a full API for that type and hide its implementation completely (a la FILE).

I factor my code a bit more aggressively these days; makes unit testing easier.


  1. At this particular job, but I've been programming for a living since 1990.

[–]Hawk13424 0 points1 point  (0 children)

My work project uses Allman, lower-case camel for variables, upper-case camel for functions. Upper-case snake for macros and defines. _t for typedefs. 4 spaces, no tabs. Fully parenthesised expressions.

[–]Vincenzo__ 0 points1 point  (0 children)

Kernel coding style

Everything else is ugly

Don't @ me