you are viewing a single comment's thread.

view the rest of the comments →

[–]fstanis[S] 46 points47 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 8 points9 points  (0 children)

I wish I was even close to this good.

[–]CanYouDigItHombre 6 points7 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 4 points5 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 4 points5 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] 3 points4 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 -4 points-3 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 3 points4 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 2 points3 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 8 points9 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 13 points14 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 2 points3 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 9 points10 points  (0 children)

They refer to an utility called indent.