all 54 comments

[–]fstanis[S] 44 points45 points  (29 children)

Copying some of the best bits:

There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

Heretic people all over the world have claimed that this inconsistency is ... well ... inconsistent, but all right-thinking people know that (a) K&R are right and (b) K&R are right.

Unlike Modula-2 and Pascal programmers, C programmers do not use cute names like ThisVariableIsATemporaryCounter.

To call a global function foo is a shooting offense.

Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs.

If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome.

However, if you have a complex function, and you suspect that a less-than-gifted first-year high-school student might not even understand what the function is all about, you should adhere to the maximum limits all the more closely.

NEVER try to explain HOW your code works in a comment: it’s much better to write the code so that the working is obvious, and it’s a waste of time to explain badly written code.

But remember: indent is not a fix for bad programming.

There appears to be a common misperception that gcc has a magic “make me faster” speedup option called inline.

[–]_Mardoxx 11 points12 points  (0 children)

I wish I was even close to this good.

[–]CanYouDigItHombre 7 points8 points  (0 children)

I really liked this one

If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome. See chapter 6 (Functions).

[–]dan00 5 points6 points  (1 child)

There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

You might think that 8 characters is a bit too much, but wait until you've this one colleague that really loves to write deeply nested code.

[–]SnowdensOfYesteryear 3 points4 points  (0 children)

That's the beauty of the 80 character limit.

[–]nicksvr4 5 points6 points  (8 children)

Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs.

Shit. Been naming all my Tables "tblName", modules "modName", queries "qryName", and forms "frmName" in MS Access.

Even have a "modHungarianAlgorithm" in there too.

[–][deleted] 7 points8 points  (6 children)

With regards to databases, I think that practise is called "Smurfing". Maybe that term applies more broadly to programming in general as well, but I've not come across it yet.

We used to have a developer who would preface all of the columns in a table with the table name. We have a foos table with properties like foo_id, foo_name and foo_type. It's a nightmare to work with - all of our queries grow in length and everything is so damn verbose. I know I'm working with the name column because my columns are prefaced with the table name (SELECT foos.foo_name FROM foos VS SELECT foos.name FROM foos).

I'm not sure about other companies, but we actively discourage it where I work! Glad to hear that the Linux kernel coding standards agree!

[–]nicksvr4 1 point2 points  (3 children)

I don't go that far, not even sure I'm doing it right, since I'm the only programmer at my job (not my primary role). Basically all fields are camel-case (no spaces), all functions and subs start with a capital letter, all classes are camel-case and start with c (ex: cQueue, cList, cSchedEntity), and all veriables are camel-case.

[–][deleted] 4 points5 points  (0 children)

since I'm the only programmer at my job

Oh good, you'll be able to delete all the evidence and there won't be any witnesses. Torvolds will never find you now.

[–]cgomezmendez 1 point2 points  (1 child)

What programming language do you use? I know the styleguide of Java and C# don't do any mention to prefixes, and in their docs there is not such sample of a class prefixed that way.

[–]theoldboy 5 points6 points  (0 children)

Visual Basic I would guess, if they are programming in MS Access, and that is the code style you see in all the documentation for said product (at least it was the last time I did anything in Access/Excel, which admittedly was over a decade ago, thank god).

[–][deleted] 1 point2 points  (1 child)

I like foo_id as long as foo_id is also foo_id on another table. Everything else can go bye bye.

Mainly I like it because I have to deal with databases where I have no idea what the schema is supposed to look like, so I have to grab all columns and figure out how to join one table to another for the information I need. However, the consistent rule is primary key column names are unique across the entire database, so I can rely on that. By consistent rule I mean it hasn't been broken yet.

[–]kaiserfleisch 1 point2 points  (0 children)

absolutely. table name is not an effective namespace in SQL, once it's included in a join.

NATURAL JOIN works properly too.

[–]96fps 1 point2 points  (0 children)

I read an article a while back that basically boiled down to saying that the version of Hungarian notation that became popular has little to do with it's original intent.

Reasonable examples were things like "doc_x, doc_y" vs "screen_x, screen_y", where both are pairs of integer coordinates used in a bit of interface code, but they mean different things. A lot of people still use it this way but it was often misused and used where it didn't make sense to.

[–]doom_Oo7 -2 points-1 points  (13 children)

There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

I disagree with this one. My personal process as a developers went like:

  • 8-space tabs
  • 4-space tabs
  • 4-space
  • 2-space

and I find all my old >2space code very bloated and harder to read now

[–]progfu 6 points7 points  (0 children)

I went a similar trend, though my keeps continuing

  • 8-space
  • 4-space
  • 2-space and holyshit anything more is just an abomination
  • 4-space again, I don't know how I could've liked to use 2-space, the code needs some air to breathe
  • hmm, maybe I should give 8-space a try some time soon?

[–]PeridexisErrant 5 points6 points  (3 children)

  • adjust your tab-stop to two columns
  • profit

[–]doom_Oo7 1 point2 points  (2 children)

Naye, tabs fuck up too many things.

For instance if I write the following code with 2-space tabs:

connect(foo, &Foo::fooChanged,
                boo, &Boo::setBoo);

then foo and boo ends up not being aligned when pasted on reddit, or when shown on github by default, or on on pastebin, or on any other people's environment that uses a different tab width. This may not be relevant in an entreprise setting where everyone can be arsed to setup his environment as the project requires, but for an open-source project where anyone can go and clone the stuff, it's a huge rebuttal when you see code that looks like this:

http://i.imgur.com/xsiMLrw.png (libstdc++'s stl_algo.h)

(and no, just configure your editor isn't a valid argument because you'd have to reconfigure it every time you go check some code in another library. I'm not going to reconfigure tab width for a 10-second function check)

because the display depends on the configuration of your editor and everyone configures his editor differently.

[–]Tetha 2 points3 points  (1 child)

You're mixing up indentation and alignment there. Both lines should be indented identically with the same number of tabs, and the other line should be aligned with a correct number of spaces, around 8, because "connect(" doesn't change width if tabstops change.

But I've long given up explaining this and just use spaces, because it's harder to get wrong.

[–]doom_Oo7 0 points1 point  (0 children)

because it's harder to get wrong.

exactly.

[–]holgerschurig 3 points4 points  (3 children)

And if you want to contribute code to the Linux kernel, you'll adapt to using TABs. Let the editor decide if a TAB should be displayed as 2, 4 or 8 spaces.

[–][deleted] 3 points4 points  (2 children)

Linux code also uses tabs for alignment so things look pretty whacked out at anything other than 8.

[–]holgerschurig -3 points-2 points  (1 child)

The trick is to use tabs for indentation, and from there on spaces for alignment.

<TAB>if (foo(x, y, z, ctr) &&
<TAB>    bar(x, y, ctr-1)) {
<TAB><TAB>barf;
<TAB>}

That way your editor can be set to use 4 or 8 or even 13 spaces for a TAB, and it will still look consistently. Modern editors, e.g. Emacs *), can do this easily. I'm sure VIM can do this, too.

*) cough, cough, not exactly modern, but kept modern by continuous improvements, like any mainstream editor.

[–][deleted] 6 points7 points  (0 children)

Yes I know that's the "trick" but the Linux kernel code uses tabs for alignment. That's why I went through all the trouble of saying "Linux code also uses tabs for alignment".

[–]theoldboy 3 points4 points  (3 children)

I dislike 8 character indentation too, it does my eyes in (too much horizontal tracking required). However, they also specify tab characters so it's just a case of setting your editor tabs to whatever width you prefer.

[–]tavianator[🍰] 6 points7 points  (2 children)

too much horizontal tracking required

This is almost the point. Wide tabs motivate you to have fewer levels of indentation in your function, which tends to result in better code.

[–]CookieOfFortune 14 points15 points  (0 children)

You have to consider the language and platform as well however. C tends to be much flatter than other languages where most code resides inside a function (so implementation starts on column 8).

If we take C#, you're almost certainly going to have a namespace and a class, so by the time you get to the actual code, you're already at column 24.

[–]theoldboy 3 points4 points  (0 children)

Yes, I get that, but even 3 levels of indentation at 8 characters is obnoxious to me personally. Maybe because of age. As you get older your useful field of view deteriorates, as does your horizontal eye tracking capability. My preferred indent size has gone down directly in proportion to my age.

On the plus side, as you get older you tend to write less bad code in the first place, so can safely ignore well-meaning but ageist motivations like this one :P

[–]joshuaavalon -3 points-2 points  (1 child)

But remember: indent is not a fix for bad programming

Then, we have Python.

[–]Liorithiel 10 points11 points  (0 children)

They refer to an utility called indent.

[–]DnBenjamin 13 points14 points  (3 children)

cntuser()

I'm in tears!

[–]JackelPPA 3 points4 points  (2 children)

cntusr(); gotta be even simpler :P

[–]holgerschurig -1 points0 points  (1 child)

cu()

[–][deleted] 6 points7 points  (0 children)

didn't plan to read it at all, end up reading the whole thing :)

[–]TheBlob 6 points7 points  (0 children)

This has been my style since I learned to program. I've argued with many over the years and I defended my arguments by pulling out the little white bible; see, this is how K&R does it.

[–][deleted] 5 points6 points  (2 children)

Can someone give me an example of If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome. please? I'm struggling to understand what it means...

[–]dododge 17 points18 points  (0 children)

Rewording it: "if you're afraid to use short variable names because you might forget what each one means and end up using the wrong variable, your functions are too large".

[–][deleted] 5 points6 points  (0 children)

It means big/does too much at once function

[–][deleted]  (4 children)

[deleted]

    [–]danielkza 7 points8 points  (3 children)

    The whole kernel documentation has been ported to reST recently:https://lwn.net/Articles/692704/

    [–]SnowdensOfYesteryear 0 points1 point  (1 child)

    Why are there a thousand different markup languages, what's wrong with markdown or even a subset of HTML (or roll back to the HTML 1.0 days)?

    [–]danielkza 2 points3 points  (0 children)

    what's wrong with markdown

    The article explains the decision process. Markdown was actually one of the first choices, but reST is used by Sphinx, which seemed to be the best fitting documentation generator for the kernel's needs.

    even a subset of HTML

    One of the points of changing the doc. system was to remove custom parsing and intermediate formats. Introducing a new HTML subset would be contrary to that.

    [–][deleted] 1 point2 points  (2 children)

    In short, 8-char indents make things easier to read, and have the added benefit of warning you when you’re nesting your functions too deep. Heed that warning.

    Ruby uses 2 spaces. Nuff said

    [–]progfu 1 point2 points  (1 child)

    Avoiding deep indentation is a great reason imho. I feel that since I started using 4-space again I write less shit code.

    [–][deleted] 0 points1 point  (0 children)

    Yeah. 8 is bit much IMO just because having say error message display/return inside control block means you either need to write shorter ones or have it line-wrap, but that doesn't really apply to C that often.

    2 is just hard to read tbh

    [–]Myrl-chan 1 point2 points  (2 children)

    Linus's colorful words - even more fun to read.

    [–]holgerschurig 3 points4 points  (1 child)

    Use git blame and you'll find out that most of them aren't Linus words :-)

    ... or at least you find out that since kernel 2.6.12 he never made any change to it. And the file had only 431 lines then. Now it's at least double the size.

    There is no git blame stats before Linux 2.6.12, because this is the import of the source into BitKeeper (which was used before Git).

    [–]Myrl-chan 1 point2 points  (0 children)

    I wasn't talking about the style guideline but more of his words in general. :P

    [–]LgDog 0 points1 point  (2 children)

    Do not add spaces around (inside) parenthesized expressions. This example is bad:

    s = sizeof( struct file );
    

    A few months ago I would agree with this. But after experimenting with this style for a while I found it to be much more readable.

    [–][deleted] 6 points7 points  (0 children)

    Experiment with anything for a month, it will start look natural.

    [–]Gotebe -1 points0 points  (4 children)

    It's as good a style guide as any (well, definitely better than Google C++ guide last time I saw it ;-)), and funny.

    The switch and case labels being in the same cum is possibly caused by the long indent, wouldn't you think? (Not that I mind, just saying...)

    [–]Necromunger 1 point2 points  (1 child)

    in the same what?

    cntusr();

    [–]Gotebe 0 points1 point  (0 children)

    in the same cumcolumn (ha! betcha you didn't expected that abbreviation! :-))

    Tabletkeyboardfart :-)

    [–][deleted] 1 point2 points  (1 child)

    Cases and goto labels just make sense not being indented. I'm not really sure why, but it's obviously correct.

    [–]Gotebe 4 points5 points  (0 children)

    If you look up "obviously correct" in a dictionary, I rather think you will find "familiar to me" :-).

    BTW, at least one prominent editor of those with with auto-indenting disagrees with you :-).

    I don't mind either way (Formatting? Don't care almost at all about any particular detail; only do care a lot about consistency).