If you can read this, either the style sheet didn't load or you have an older browser that doesn't support style sheets. Try clearing your browser cache and refreshing the page.

(IT World)   The optimal coding indentation style? Easy. Tabs that are 84% soft and 3.3 columns wide   (itworld.com) divider line 59
    More: Obvious, line coding, 1TBS  
•       •       •

2070 clicks; posted to Geek » on 06 Mar 2013 at 9:43 AM (1 year ago)   |  Favorite    |   share:  Share on Twitter share via Email Share on Facebook   more»



59 Comments   (+0 »)
   
View Voting Results: Smartest and Funniest

Archived thread

First | « | 1 | 2 | » | Last | Show all
 
2013-03-06 09:47:02 AM
The optimal indentation style is to be flexible and not sweat little stuff so much.
 
2013-03-06 09:49:56 AM
We have one coding-style zealot we work with, and we all give him grief about things like indentation, open/close bracket location, TODO/HACK/FIXME tags, etc.  I think we'll eventually give him an embolism.
 
2013-03-06 09:51:37 AM
Best is to mix up spaces and tabs, and never use the same number of either.
 
2013-03-06 09:54:02 AM
The optimal coding indentation style is to remember that the compiler doesn't see white-space and just do whatever gives you and your co-workers the easiest time distinguishing loops, functions, relevant blocks, etc.

If you're taking the time to argue about it you're literally wasting more time than you would just staring at your co-worker's code for thirty seconds to get used to it.
 
2013-03-06 10:04:02 AM
I prefer to write my code in a gigantic single line. Screw you, readability! As long as it compiles, it's good!
 
2013-03-06 10:05:19 AM
Use a formatter.
Apply it before and after all changes and before commits to the repo.
Changes are uniform and indent style is consistent.

/Not loved, consistent
 
2013-03-06 10:08:15 AM

Jim_Callahan: The optimal coding indentation style is to remember that the compiler doesn't see white-space and just do whatever gives you and your co-workers the easiest time distinguishing loops, functions, relevant blocks, etc.

If you're taking the time to argue about it you're literally wasting more time than you would just staring at your co-worker's code for thirty seconds to get used to it.


So... you've never worked on a development team before, i take it?  This is something that gets discussed at length, and it's better for everyone on a team if everyone on the team uses the same style.  Even when we're all doing that, it's mundane chatter to discuss it, the same way you and your co-workers might discuss the proper way to make the french-fries.
 
2013-03-06 10:10:07 AM
Sometimes
 I use one space
  especially if I know
    my nests are going 8 deep

Otherwise
   I only use a triple space
 
2013-03-06 10:11:57 AM
I was born a soft tab, two columns man, and I'll die a soft tab, two columns man.

But really, the correct answer is "whatever everyone else on the project is doing."
 
2013-03-06 10:22:16 AM
IronJelly:So... you've never worked on a development team before, i take it?  This is something that gets discussed at length, and it's better for everyone on a team if everyone on the team uses the same style.  Even when we're all doing that, it's mundane chatter to discuss it, the same way you and your co-workers might discuss the proper way to make the french-fries.

So you agree with my general point, then insult me for being wrong?  With reading comprehension skills like that, no wonder you have to discuss this "at length" instead of exchanging like three or four seconds of dialogue or a short memo to get everyone on the same page.
 
2013-03-06 10:27:24 AM
If you use anything other than hard tabs, fark you.

Hard tabs mean each developer can set tab width to whatever he wants.

Soft tabs mean you get that one dipshiat who insists on 5 spaces instead of 4 and farks up the whole codebase.
 
2013-03-06 10:27:48 AM

Jim_Callahan: IronJelly:So... you've never worked on a development team before, i take it?  This is something that gets discussed at length, and it's better for everyone on a team if everyone on the team uses the same style.  Even when we're all doing that, it's mundane chatter to discuss it, the same way you and your co-workers might discuss the proper way to make the french-fries.

So you agree with my general point, then insult me for being wrong?  With reading comprehension skills like that, no wonder you have to discuss this "at length" instead of exchanging like three or four seconds of dialogue or a short memo to get everyone on the same page.


Wow.  ok, let me explain this to you.

you said this:

Jim_Callahan: If you're taking the time to argue about it you're literally wasting more time than you would just staring at your co-worker's code for thirty seconds to get used to it.


To which my reply is that on the few teams I've been on, it's been a topic of discussion for more than your 30 seconds,  I don't think i agreed with your general point at all.  Then I insulted you for commenting like you know my industry when you clearly don't.  With reading comprehension skills like that, no wonder you think it's a mundane discussion, when you can't see the long-term impact that this decision can have.

And your profile is nearly empty, so I have to assume you work in some profession that doesn't involve computer skills.
 
2013-03-06 10:28:07 AM
I don't care if someone uses soft tabs. I did until recently. However, if you're INSISTENT on using soft tabs in a team environment, you're an asshole. If you use hard tabs, everyone can set their tab width to whatever is comfortable for them and everyone's happy. Not to mention it's (trivially) more efficient since you're storing one character instead of 2, 3, 4, 6, 8 depending on your tab width preference.
 
2013-03-06 10:31:04 AM

Mayhem of the Black Underclass: Sometimes
 I use one space
  especially if I know
    my nests are going 8 deep

Otherwise
   I only use a triple space


If your nests are going 8 deep, you need to break that out into a function or something. Should be no reason to have anything nested more than 3 or 4 deep, tops.

On the other hand, that was a pretty nice poem!
 
2013-03-06 10:37:41 AM

Olympic Trolling Judge: I was born a soft tab, two columns man, and I'll die a soft tab, two columns man.

But really, the correct answer is "whatever everyone else on the project is doing."


Correct answer (2 cols, soft)
And as long as everyone else on the project is doing it my way we're fine.
 
2013-03-06 10:56:28 AM

servlet: I don't care if someone uses soft tabs. I did until recently. However, if you're INSISTENT on using soft tabs in a team environment, you're an asshole. If you use hard tabs, everyone can set their tab width to whatever is comfortable for them and everyone's happy. Not to mention it's (trivially) more efficient since you're storing one character instead of 2, 3, 4, 6, 8 depending on your tab width preference.


Yeah, I've always used soft tabs, just because it is the default on my preferred IDE (Komodo). But I don't get the argument against it by saying it is more consistent across operating systems. I mean yeah, it will be slightly different in terms of the number of columns but that isn't too big a deal, and really what we should be concerned about is how it looks to another developer. If they are opening your code in an IDE or coding-specific text editor, they will have set what tab defaults to for number of columns. Seems a better practice to me these days.
 
2013-03-06 10:57:44 AM

IronJelly: Jim_Callahan: IronJelly:
And your profile is nearly empty, so I have to assume you work in some profession that doesn't involve computer skills.



I'm genuinely curious about how people feel about this. My first reaction was that Iron Jelly's post was a complete non-sequitor, but I'm willing to learn if others have a different background.

I work on patient data (statistician, not programmer). Before publishing anything, you need to make sure that it is impossible to identify the patients involved. I relax this somewhat in my posts (e.g. revealing the fact that I'm a statistician just now), but still my first instinct is to not make information available unless it is relevant. The programmers (I'm lumping a lot of disciplines together with this word, I realize) and sysadmins I've worked with have all been very careful with data security as well. I've tended to associate willingness to share information on-line either with someone in a field where politics is an essential part of the job, or with someone who is a lot younger than I am (e.g. high-school and college).

I'm trying to figure out what kind of programmer does not have the instinct to keep from sharing non-essential information with the general public. Have you worked on dev teams for very public, open-source projects that had no security concerns? How about other programmers here?

/1 space because SAS encourages a lot of nesting. Drives the programmers up the wall . . .
 
2013-03-06 11:02:55 AM
Just base your code on the excellent examples shown by the winners of the The International Obfuscated C Code Contest  and you'll do fine, and your coworkers will appreciate your thoughtfulness.
 
2013-03-06 11:18:58 AM
http://www.python.org/dev/peps/pep-0008/#indentation

4 SPACES!

People using tabs or any number of spaces other than 4 should be shot.
 
2013-03-06 11:25:00 AM
I just use what ever the default is in what ever dev environment I happen to be in at the moment. So long as the formatting style is internally consistent, I really don't care.

The only thing which REALLY bugs me as a programmer is when people put their opening { at the end of the line, instead of on it's own line, but that's just how I learned.

Pretty much the only thing in my entire life which is neat and organized is my code, everything else is a chaotic mess.
 
2013-03-06 11:38:41 AM

Jim_Callahan: the compiler doesn't see white-space


www.python.org
 
2013-03-06 11:39:19 AM

Quantum Apostrophe: Jim_Callahan: the compiler doesn't see white-space

[www.python.org image 211x71]


Also

rubyonrails.org
 
2013-03-06 11:50:24 AM

JustFish: The only thing which REALLY bugs me as a programmer is when people put their opening { at the end of the line, instead of on it's own line, but that's just how I learned.


As displays continue to get wider and wider, and vertical resolution remains stubbornly stagnant, I'm going to stick with the more compact style. It took me a while to get used to it, too, but it was a worthwhile investment when 24-line displays were the norm, and we've made disgustingly little progress since then.
 
2013-03-06 12:22:06 PM
Nevermind the discussion about tabs, that never goes anywhere beyond acknowledging that the right answer is whatever the standard for the project is...

Why does his list of 'popular languages' not include C?  C#, Objective-C, C++, even bloody pascal and fortran, but not just plain C?
 
2013-03-06 12:34:55 PM

JustFish: The only thing which REALLY bugs me as a programmer is when people put their opening { at the end of the line, instead of on it's own line, but that's just how I learned.


What blows my mind are people who spend their entire lives around the meaning of obscure symbols but can't tell its from it's.
 
2013-03-06 12:35:51 PM

JustFish: The only thing which REALLY bugs me as a programmer is when people put their opening { at the end of the line, instead of on it's own line, but that's just how I learned.


I'm the exact opposite. It seems like such a waste of a line and I find it aesthetically displeasing.
 
2013-03-06 12:49:56 PM

Quantum Apostrophe: JustFish: The only thing which REALLY bugs me as a programmer is when people put their opening { at the end of the line, instead of on it's own line, but that's just how I learned.

What blows my mind are people who spend their entire lives around the meaning of obscure symbols but can't tell its from it's.


That's a good point, but coding is WAY more terse and specific then english. The rules as aren't arbitrary, and when I post comments, I don't get compiling errors, so nothing pushes me to actually make changes.... aside from snarky comments.

entropic_existence: JustFish: The only thing which REALLY bugs me as a programmer is when people put their opening { at the end of the line, instead of on it's own line, but that's just how I learned.

I'm the exact opposite. It seems like such a waste of a line and I find it aesthetically displeasing.


Well, since all white space is ignored, there's no such thing as a waste of line, but I do know people who make it a contest to have as few "lines of code" as possible. Of course, with the many languages that use ; or something similar as EOL, you could easily make completely unreadable code on a single line.

Again, just preference, but since many of the environments I code in allow scroll wheel to zoom on code, it's easier for me to find matched {} for copy or delete if they have their own lines.
 
2013-03-06 01:17:07 PM

JustFish: Well, since all white space is ignored, there's no such thing as a waste of line, but I do know people who make it a contest to have as few "lines of code" as possible. Of course, with the many languages that use ; or something similar as EOL, you could easily make completely unreadable code on a single line.

Again, just preference, but since many of the environments I code in allow scroll wheel to zoom on code, it's easier for me to find matched {} for copy or delete if they have their own lines.


Yeah for me it isn't about getting as few lines as possible, after all line breaks and comments make code easier to read and understand. I just find that to me, it looks odd and finding matches }'s with indents is just as easy for me. But when I learned to code I learned with not putting opening {'s on their own line, so it is really just a preference.

I also don't worry about anything to do with compilers, as I don't regularly code in compiled languages.
 
2013-03-06 01:19:56 PM
Let's get this rolling! Hopefully fark doesn't destroy my creation

**/
* does foo
*/

public void doFoo()
{
//imagine these are hard tabs
println("foo\n");
}
 
2013-03-06 01:38:01 PM
CSB: I had a professor in school who, on day one, asked, "which do you prefer, { on the same line or on the following line?" About half the hands went up for each. "Alright, good. In this class, the code style will be: on the same line for function/object declarations, but on the following line for all control flow statements. This way, you can ALL learn what it's like to conform to a company standard you hate. This will factor into your project grades."

/then just a sort of horrified silence
 
2013-03-06 01:44:55 PM

randroid: Let's get this rolling! Hopefully fark doesn't destroy my creation

**/
* does foo
*/

public void doFoo()
{
//imagine these are hard tabs
println("foo\n");
}


Ctrl+A
Ctrl+I

/hope the people I work with aren't secretly mad I do this to every file I touch
 
2013-03-06 02:08:48 PM
I should be able to view the code as I please. Check it in in a white-space independent format. Languages that are white-space sensitive can die.
 
2013-03-06 02:20:48 PM
I hate coding standard discussions.

You can bring the entire meeting to a halt about where to put curly brackets.

Do you allow this for single line

if( m_bIsItADuck == true )
    x = 1;

or this
if( m_bIsItADuck == true ) { x = 1; }

or this
if( m_bIsItADuck == true ) {
    x = 1;
}

or this
if( m_bIsItADuck == true )
{
    x = 1;
}

And that's not even getting into should there be white space between a "(" and the "m_bIsItADuck".

Oh, and do we even to the m_bIsItADuck to represent that it is a member variable that is of type boolean.

Oh, and do we say if( m_bIsItADuck == true ), or just if( m_bIsItADuck )?  What about if it is not a duck?

GAHHHHHHHHH!

//And what about using the conditional operator?
 
2013-03-06 02:21:22 PM
I don't understand why this is a debate. Are there people still coding in Notepad or something? Is there really a decent editor that can't convert between different styles?

for the record I use hard tabs, 4 spaces in every language I code in. Even in Python where I'm told it's the greatest of all sins.
 
jvl
2013-03-06 02:26:03 PM
"most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton
 
2013-03-06 02:27:03 PM
Oh, and for the record, our team "officially" uses 4 spaces for an indent, but it's not enforced, as I see the mix constantly.

Only once has it been a complaint, because a new guy checked in some code he had auto corrected to switch from spaces to tabs and it pissed of one of the senior guys that saw the change using a compare tool afterwords.
 
jvl
2013-03-06 02:38:24 PM
It's amazing how many people confuse "what happens when I press the TAB key", "indent distance", and "how ASCII-TAB is displayed" and think they need to set hard tabs to 4.  Those are three entirely separate concepts children.
 
2013-03-06 02:48:14 PM

meat0918: Do you allow this for single line

if( m_bIsItADuck == true )
x = 1;


No no no no. Even without the "add a line under it and not notice it's not covered by the if-statement" problem, you'll have to worry about stuff like, "function that's actually a macro and expands to two lines that for some reason aren't surrounded by {} in the definition" type stuff. There's never a good reason to use single-line if like that.

All the others are livable.
 
2013-03-06 02:51:48 PM

ProfessorOhki: CSB: I had a professor in school who, on day one, asked, "which do you prefer, { on the same line or on the following line?" About half the hands went up for each. "Alright, good. In this class, the code style will be: on the same line for function/object declarations, but on the following line for all control flow statements. This way, you can ALL learn what it's like to conform to a company standard you hate. This will factor into your project grades."

/then just a sort of horrified silence


I love everything about this.

I end up working with so many different languages over the course of a month, many with very different requirements and forgiveness on various formatting points, I'm just happy it compiles most the time.

The good part is, I'm the only one who ever touches my code, and very rarely have to work with other peoples files. Our company tried a colaberative project once or twice, but it was a complete failure.

The things that trip me up the most often are EOL characters, the different ways HEX vs ASCII characters are handled.

Some languages don't care about EOL, others require them. Some are context sensitive on == vs = (comparitor versus assignment), etc.

I spend almost as much time, hell, sometimes more time, looking up language specific standards, or the specific spelling of common functions in that particular language. Code flow, parsing techniques, check sum functions, etc, I use basically the same writing style which every language I'm in, it just really pisses me off some times trying to convert functionality from my "base languages" into my secondary, tertiary, or worst, "We've decided we're using this on this one specific project and will never likely use it again" language.
 
2013-03-06 02:56:28 PM
HARD TABS MUST BE BANISHED FROM THE SPACE OF WHITE!!

the length doesn't matter though as long as it's consistent (2-4 spaces is reasonable)
 
2013-03-06 03:09:15 PM

ProfessorOhki: meat0918: Do you allow this for single line

if( m_bIsItADuck == true )
x = 1;

No no no no. Even without the "add a line under it and not notice it's not covered by the if-statement" problem, you'll have to worry about stuff like, "function that's actually a macro and expands to two lines that for some reason aren't surrounded by {} in the definition" type stuff. There's never a good reason to use single-line if like that.

All the others are livable.


I had to fix that exact scenario recently shortly after the guy who did it retired....

 
2013-03-06 03:12:42 PM

meat0918: Do you allow this for single line

if( m_bIsItADuck == true )
x = 1;

or this
if( m_bIsItADuck == true ) { x = 1; }

or this
if( m_bIsItADuck == true ) {
x = 1;
}

or this
if( m_bIsItADuck == true )
{
x = 1;
}


I would say:

* Never acceptable style
* Always acceptable style, as long as the statement is short
* Acceptable if this is the norm for the code base
* Acceptable if this is the norm for the code base, except always unacceptable in JavaScript

I prefer style 4 for anything except conditional returns (which I use style 2).  Style 3 is what I do in JavaScript due to how its parser works.
 
2013-03-06 03:52:19 PM
Sum Dum Gai:

Style 3 is what I do in JavaScript due to how its parser works.

Que?  The ECMA-262 spec allows for a line terminator between any two tokens and also the right paren/left curly brace token sequence is not affected by automatic semicolon insertion.

Personal preference (or company coding standards) are still the only reasons in play here.
 
2013-03-06 04:08:13 PM

IoSaturnalia: Que? The ECMA-262 spec allows for a line terminator between any two tokens and also the right paren/left curly brace token sequence is not affected by automatic semicolon insertion.

Personal preference (or company coding standards) are still the only reasons in play here.


It is affected in the case of an object literal being returned from a method.  The following:

var foo = function(){
   return
   {
      foo: "bar"
   }
}

actually returns null, not an object with property "foo" set to value "bar".  Since that construct must have the opening brace on the same line as the return, it's better style to keep all of the opening braces consistent; it reduces the chance you'll type a return statement wrong.
 
2013-03-06 04:48:57 PM
Your example says a lot more about the return statement than it does the curly brace.  Returning just the string literal on two lines has the exact same problem without a curly brace in sight:

return
"bar";


Your example is more of an argument to just always be aware of the evils of automatic semicolon insertion.  Automatic semicolon insertion has no place in a 'c'-like language, but we are stuck with it.
 
2013-03-06 05:06:42 PM
The only thing worse than shiatty standards is not having any standards. I once worked at a company where "edit wars" over coding style were a daily occurrence. Complete waste of time.
 
2013-03-06 05:24:53 PM

IoSaturnalia: Your example says a lot more about the return statement than it does the curly brace. Returning just the string literal on two lines has the exact same problem without a curly brace in sight:


Yes, but it's common enough to return an object literal, and keeping the curly brace style consistent between this case and every other case of curly braces makes it less error prone.
 
2013-03-06 05:29:50 PM

jonny_q: If you use anything other than hard tabs, fark you.

Hard tabs mean each developer can set tab width to whatever he wants.

Soft tabs mean you get that one dipshiat who insists on 5 spaces instead of 4 and farks up the whole codebase.


Hard tabs mean that the code looks like ass if I use a different tab-stop than anyone else, so you end up having to standardize on that instead of the number of spaces.  Totally useless solution.

This comes up when you do things like break argument lists onto separate lines (for grouping by meaning or because the line would be too long, etc.) or in chained function calls, etc.

If you have one dipshiat who's using a different style than everyone else, your project is doomed anyway.  If you can't even sit down and come up with a standard and have it at least vaguely followed, you're not going to manage to work together on the important stuff.
 
2013-03-06 05:55:05 PM

Sum Dum Gai: keeping the curly brace style consistent between this case and every other case of curly braces makes it less error prone.


I dunno.  Sounds kinda cargo-cultish to me.

Blindly using an anti-pattern like unsymmetrical block delimiters merely as a prophalactic against a well-known language feature is going to be  more error prone than actually knowing how the language works and coding accordingly.
 
2013-03-06 06:38:49 PM

jonny_q: If you use anything other than hard tabs, fark you.

Hard tabs mean each developer can set tab width to whatever he wants.



This.

Though I've heard some people say that if you use spaces it ensures that the code looks the same on everyone's machine. To which I've always thought: What's so special about that?
 
Displayed 50 of 59 comments

First | « | 1 | 2 | » | Last | Show all

View Voting Results: Smartest and Funniest


This thread is archived, and closed to new comments.

Continue Farking
Submit a Link »






Report