Fog Creek Software
Discussion Board




Indentation

...the quickest way to start an ideology war?

After looking at some older code that has been maintained by more than one of our group, it has become obvious that we need to get serious about developing (well, okay, following) our internal style guides, if only to reduce the blood pressure of the chief code architect ;)

Our main application is written in C++, but the stuff that really matters in this context is SQL and HTML.  I say this is what really matters because it's primarily what the fakin' it programmers like me work on, and we are the ones who are (mainly) messing up the styles.

I did a little research (and I do remember something from my higher education days) on the matter.

Of the three main contenders for "normal" indentation styles, there are:

while (x = 1) {
something;
}

while (x = 1)
{
something;
}

while (x = 1)
{
something;
}

Option 1 is a non-starter - I've been told "let's not go there"...

What do people think about the relative merits of Options 2 and 3, keeping in mind the use for writing HTML and SQL queries? 

Also, is there a way to make sticking to the guidelines less painful?  Our crew sees very little advantage in terms of immediate readability, long-term readability is a historically poor motivator and it seems to me that both methods have a lot of pressing the space bar (or tab).

Phibian
Tuesday, July 20, 2004

Firstly I like option 1, but with actual indentation that you seem to be missing.

Secondly, what in the Hell is so hard about hitting tab or space?  Seriously?  If you are a programmer and cannot type, switch jobs.

muppet
Tuesday, July 20, 2004

ahh, the holy wars...

I prefer my opening curly braces to be on a line of their own. :)

Ankur
Tuesday, July 20, 2004

My god man, that infinite loop clearly is a more pressing matter.

Seriously, though, it seems that it was a mistake for tools to ever save and load plain text -- tools should save a parse table and reformat it in whatever screwed up idiosyncracy an individual developer has. The fact that there are still debates over indentation or bracket placement is just a sad, sad reflection on this profession.

Dennis Forbes
Tuesday, July 20, 2004

Why do people seriously think there is something to be gained here?

If the code you're modifying follows style 1, then keep it style 1.

If the code you're modifying follows style 2, then keep it style 2.

What is so fucking difficult about that?

void* where( prohibited )
Tuesday, July 20, 2004

Echo Mr. Forbes, although it is amusing that, on Firefox anyway, there is no indentation in the above lines at all  thus showing that there is neither semantic nor presentational difference between 2 and 3.

As for option 1, that was the normal style when C were nowt but an 8bit lad. 

Surprisingly, I'm able to read all of them equally well and more importantly so is the compiler.

Simon Lucy
Tuesday, July 20, 2004

> What is so fucking difficult about that?

Started a new job, I follow the indentation style they use which is

    if (xxx)
    {
        zzzzzz;
    }

Different source files have different indentations some 4 some 2 some 8. I just do what the file has.

Oh, and
    if (xxx) {
        zzzz;
    }

is what Sun recommends for Java. The indents are 4 spaces.

Tabs, if you use them, must be for 8 spaces.

just a programmer
Tuesday, July 20, 2004

I agree with "just a programmer".  An old saying about what to do when you are in Rome comes to mind.

Foolish Jordan
Tuesday, July 20, 2004

If the computer can do it, why should you?  Use a good editor that indents for you and let yourself forget about it. 

Brent
Tuesday, July 20, 2004

I know it for sure that all real programmers, myself included, always prefer the first option. When I see #2 or #3 in code samples which I review a lot, it's an immediate no hire.

Egor
Tuesday, July 20, 2004

How about using a code beautifier.  That way it can be run as a nightly job and you never have to worry about it ever again.  It is rather important to have everyone w/ the same tab size and indentation size, if they're using a fancypants editor.  Oh, make sure the beautifier works in some odd cases like nested if/else if/else if/else clauses.  We had one, JPretty I believe, that indented every subsequent if clause another tabstop.  Another thing to remember is that if your tabs aren't big enough, then the code runs together, if they're too big then your code runs off the edge of the editor. 3 or 4 seems to be a good indent size.

On the note of Sun's Java code, I hate it.  I believe tabs are the only thing that should be used to indent code, 'cause otherwise you have to worry about pressing the tab key once and the space bar 4 times.  That's dumb in my opinion.

Ryan Phelps
Tuesday, July 20, 2004

Indentation and brackets matter becauseIT'smucheasier to re ad code (oranytextforthat_matter 
) when YoU

              follow commoncOnVentions
.

IME mixed conventions in a large project are a warning sign of a disjointed team.

---

Re. > What is so fucking difficult about that?
For one thing, unless you're editing in notepad.exe then you need to reconfigure your editor 'smarts' and macros to conform to the new style.

_
Tuesday, July 20, 2004

After reading and writing much code, my preferred indentation method is:

if (condition) {
        statement;
        };
another statement here;


The basic idea being that indentation best indicates nesting levels to humans, while indentation for compilers, { and }, should be treated as just white space.

CWallach
Tuesday, July 20, 2004

Well, I have seen lots of developers working on the same source code with different editors. Mostly reindenting the existing code is not possible because your CM system doesn’t like it.
And if you are just maintaining software, your footprint will be anyway very small, so stick to the existing style.
If you want to make a nice present to co-workers, just mix tabs and spaces. Other people will love to maintain your code in their editor of choice, put that's an OPP.

zaph0d
Tuesday, July 20, 2004

Pick either 1 (classic K&R, aka the 'one true formatting'),  or 3 where your statements line up with your braces and so blocks can easily be seen. 2 is harder to read and is easier to make mistakes with.

Dennis Atkins
Tuesday, July 20, 2004

+++For one thing, unless you're editing in notepad.exe then you need to reconfigure your editor 'smarts' and macros to conform to the new style. +++

'smart' editors and IDEs are for poor programmers.  Period.

muppet
Tuesday, July 20, 2004

Code Complete has a chapter on this stuff. McConnell recommends the style 1:
  if( something ) {
      then code;
  }

Under the theory that the if and the closing brace line up, so it's immediately obvious as you scan the code where the if statement ends.

I personally prefer:
  if( something )
  {
      then code;
  }

because I find the additional white space makes it easier to navigate the code. YMMV, and in general the "do as in Rome" convention ends up working out really well.

Chris Tavares
Tuesday, July 20, 2004

A bit of historical context on the topic from FOLDOC:

http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=indent&action=Search

- former car owner in Queens
Tuesday, July 20, 2004

Follow any standard except the one that Cameron Purdy uses.

if(isSomeFlagSet())
        {
        indentEverything(true);
        }

Joe Blandy
Tuesday, July 20, 2004

> 'smart' editors and IDEs are for poor programmers.  Period.
A revealing observation.
And your editor of choice is ...?

_
Tuesday, July 20, 2004

Syn.

It has color coding, which is all anyone needs.

muppet
Tuesday, July 20, 2004

Muppet, a lot of us buy bread and milk writing code. Productivity enhancers enhance our productivity (1). Meaning we can have some butter too.

(1) Had to say that in those words. Just had to.

muppet hunter
Tuesday, July 20, 2004

"'smart' editors and IDEs are for poor programmers.  Period."

Syn looks fairly 'smart' to me.  What makes color coding an Ok feature to have and automatic formating a tool for bad programmers in your book?  Most of my work is done in vi but I do not bash people for using more specialized IDEs. 

Jon Lindbo
Tuesday, July 20, 2004

Hi Chris, the don't go there guy disagrees with McConnell because the if and the closing brace are not the same entity.  So it makes it harder to find the matches.   
In that context, the link on historical context was really interesting, especially the part about how K&R is falling into disuse!

Muppet, there's nothing difficult about hitting space, but we're lazy and dislike extra work for no reason.  If something can be automated or eliminated, that leaves that much more time to do other things.  Like feeding trolls :)

Given that we are in the process of defining "Roman traditions", comments about just following what was done are not that helpful, but thanks anyway.  Following the standards from the individual file isn't likely to lead to standardization over the long term (our goal, for those keeping score at home).  This is true because a) the files have different conventions and b) there are thousands of files.

I admit I tend to use notepad, and as such so does the rest of the team, mainly because I hate waiting for files to load since I open so many of them everyday. I have yet to find an editor that matches the opening speed of Notepad with useful additional features (eg automatic indenting, multiple level undo, pretty colours). 

I really like the code beautifier idea.  No extra spaces, would be able to automatically update all of our existing stuff, keep us all on the straight and narrow without changing our bad habits - I love it!  Anyone else using them?  Any horror stories?  Recommendations (assuming that JPretty was an anti-recommendation) ?

Of course, no one here has commented on the whole SQL / HTML main utility part - neither of which are strong on brackets...  Maybe only real programmers actually indent stuff and the rest of us wouldn't normally read this thread.

Phibian
Tuesday, July 20, 2004

"'smart' editors and IDEs are for poor programmers.  Period."

This is bloody inane. Smart editors are for smart people that care about solving edge problems, not dinking around formatting code and performing other make-work tasks all day long (anything that the compiler doesn't give a shit about, I don't give a shit about, which is why these masturbatory standards discussions are so weak). Dumb editors are for marginal programmers that would rather surround themselves with marginal tasks for the illusion of complexity.

.
Tuesday, July 20, 2004

Phibian: Vim opens up instantly (at least on my machines) and has tons of features. I've used it for a couple of years now after switching from emacs, and it's great.

sid6581
Tuesday, July 20, 2004

No identation is going to help you with the bug in the code

while ( x == 1 ) and not while ( x = 1 )

:-)

As far as I am concerned I am game for any indentation as long as it is done uniformly and if anyplace you can skip it altogether it is even better

e.g

if ( x == 3 ) y = 2 ; else y = 3 ;

better than

if ( x == 3 )
{
      y = 2 ;
}
else
{
    y = 3  ;
}

or its variations...ofcourse the ternary conditional might be the best in this case...but I digress

Code Monkey
Tuesday, July 20, 2004

Er I'll take a look at it - we were steered away from looking at Vi because of the learning curve.

Vim looks slightly less psycho but...  For example, I type :q all the time as part of the scripting platform we use, and having to think about whether I've previously typed an "i" or not isn't the kind of "feature" I'd like to gain, even for automatic indenting :)

Phibian
Tuesday, July 20, 2004

I prefer:

if ( x == 3 ) y = 2 ;
else y = 3 ;

Over anything longer.  Compact is readable.

Almost Anonymous
Tuesday, July 20, 2004

If it's all the same to the compilier, each programmer should have their own prettyprinter so that the checked in code's formatting doesn't matter.

MilesArcher
Tuesday, July 20, 2004

Miles, Nice in theory until you want to build the same binaries as your co-worker. Or you see "assertion failure on file.cpp line 254". So where is that again?

_
Tuesday, July 20, 2004

Hey, seeing those unsinglesteppable lines which force you to enter the disassembly view to step through them reminds me that nobody's mentioned that debuggers are evil and everyone who uses them is a retard.

Someone should bring that up too. It would be an interesting diversion and produce some really interesting insights.

Tom
Tuesday, July 20, 2004

Not that this discusion has been neither useful nor chock full of insights so far, of course.

Tom
Tuesday, July 20, 2004

Vim certainly takes some getting used to, but there's a reason so many people swear by it: It kicks ass! Modal editing, if you can get used to it (and many people can't, or won't), can be much more effective than normal editing.

Kinda like HP calculators. If you get used to reverse Polish notation, it's hard to go back to algebraic calculators again. I just feel crippled when I have to use normal editors and algebraic calculators. (For editors, I literally feel crippled. Modal editing is much better on the hands and fingers when you're typing code all day.)

sid6581
Tuesday, July 20, 2004

-,

Good point. However, if you run it an IDE that drops you at the error it won't matter.

What do I know, I'm a manager and part time vb programmer.

MilesArcher
Tuesday, July 20, 2004


*********************************************
Dumb editors are for marginal programmers that would rather surround themselves with marginal tasks for the illusion of complexity.

*********************************************

You hit it on the head here.

I'm the guy who wondered why I encountered so much resistance to suggestions to automate work in my project group.

frustrated
Tuesday, July 20, 2004

Actually I like

y = ( x == 3 ) ? 2 : 3;

the best.

:D
Tuesday, July 20, 2004

I don't know how any sane, rational person could find -

while(stuff){
more stuff
}

more readable than -

while(stuff)
{
    more stuff
}

Anyone who willingly employs the former is not worth spitting on as a programmer.  Further, anyone that uses spaces instead of tabs should be tortured to death in a particularly gruesome fashion.

fuel
Tuesday, July 20, 2004

+++This is bloody inane. Smart editors are for smart people that care about solving edge problems, not dinking around formatting code and performing other make-work tasks all day long (anything that the compiler doesn't give a shit about, I don't give a shit about, which is why these masturbatory standards discussions are so weak). Dumb editors are for marginal programmers that would rather surround themselves with marginal tasks for the illusion of complexity.+++

Perhaps if more programmers had the attention to detail that actually WRITING your own damn code brings you, we wouldn't se so many half-assed products on the market.

I solve 'edge' problems just fine on a near daily basis with my dinky little editor, and if something goes wrong, I don't need a watch window to tell me what, either.

I'm sorry that you feel the need to belittle your betters, but that's what happens when people are jealous of other's talent.

I don't use a stencil to paint and I don't use code-completing-self-indenting-spellchecking-syntax-correcting editors to code.

muppet
Tuesday, July 20, 2004

I am soooo special.

notepad programmer
Tuesday, July 20, 2004

notepad?  Hell no.  I'd never use notepad.  I need some color coding.

But watch windows and project files etc etc .  Fluff or added complexity.  I once spent 3 hours trying to get a project to compile in JBuilder before I got fed up and did it with javac in 3 minutes.

muppet
Tuesday, July 20, 2004

The problem with smart editors is that they're too dumb to work out which style of indenting you prefer before they start automatically indenting your code. They always pick the wrong style when I've tried them and I'm far too lazy to search through the online help to work work out how to change that.

Wayne Hayter
Tuesday, July 20, 2004

There would be a good thesis in what preferences for the different braces styles mean.

For the first two years I was using C/C++, it seemed logical to me that the braces should be part of the code block they enclose, like:

my function
{
    statements;
}

Eventually I realised that splitting the alignment, the way experienced programmers do, was much more comfortable for me. That is, like this:

experienced programmmer says {

    statements;

}


So I think in some strange cognitive way, the splitting of alignment marks a more experienced, more comfortable understanding of C++ and Java.

Me And The View Out The Window
Tuesday, July 20, 2004

Strange cognition indeed. Experienced does not necessarily equal good.  I know many bad programmers that are very experienced at writing bad code.

Good programmers value clarity.

i see dead code
Tuesday, July 20, 2004

Once again, muppet sees things one way and concludes that anyone who sees it differently is deluded.


Tuesday, July 20, 2004

I never said anyone who uses an IDE is deluded, I just said they're likely to be a mediocre programmer.

muppet
Tuesday, July 20, 2004

"I never said anyone who uses an IDE is deluded, I just said they're likely to be a mediocre programmer."

Nice OPINION...  but do you have any reasoned arguments to back that up?

I good IDE can save a lot of time constantly looking at documentation.  I, for one, wish I had one right now.  I'd be considerably more productive.

Almost Anonymous
Tuesday, July 20, 2004

Ah, religious wars.  How sweet.
Personally, I prefer:

if (x == 1)
  {
    <Do something here...>
  }

<Follow-on code>

I KNOW it's not K&R original, but I never liked K&R original.  When looking at a printed listing (like during a code review, wink, wink) its MUCH easier to match up the braces that are at LEAST on the same column as each other.  Scope of 'if()' 'while()' and case statements is much easier to see this way.

And if you format function definitions that way, why should you format 'if' statements differently?

If it is any consolation, I understand that gnu standard is as I have said, as is Linux standard.

Having said all that, I only get to do that if I code from scratch.  If I'm maintaining somebody else's code, I follow whatever they did.

AllanL5
Tuesday, July 20, 2004

I only have to constantly refer to documentation for languages I've just learned.  I've found, however, that once I've used a language for a good period of time (a year or more) I hardly refer to documentation at all.  Currently the only language I find myself consistently reading docs for is Java.

OK I admit that I use PHPEdit for PHP, but only for the color coding :)

muppet
Tuesday, July 20, 2004

"I've found, however, that once I've used a language for a good period of time (a year or more) I hardly refer to documentation at all."

I'm afraid it's not so simple -- I'm dealing with a platform with a very large API.  I hate having to constantly look up every method name and parameter list.

Almost Anonymous
Tuesday, July 20, 2004

which API is that?  I memorized the entire MFC

;)

muppet
Tuesday, July 20, 2004

I once spent an hour trying to get my manual-transmission automobile to take me to the library four blocks away before I realized I could walk there in ten minutes.

Ergo, manual transmissions are for crappy drivers.

Kyralessa
Tuesday, July 20, 2004

"It has color coding, which is all anyone needs."

You know, I have to agree with this completely.

Accountants should stop demanding spreadsheets and databases - plain text with a bit of colour will do them fine. After all, they're accountants - they

Anyone who can't keep their entire customer database in a plain text file (maybe with a bit of colour coding) is obviously incompetent.

Any coder who demands more than "syntax highlighting" is obviously not a Real Coder(TM) and shouldn't be working.

That all sounds completely sensible to me. The only real alternative is for programmers to actually have tools that are at least as powerful and useful as the tools we create for our customers, and that's obviously insane.


"Hell no.  I'd never use notepad.  I need some color coding."

Why do you feel the need to belittle your superiors? You don't need a watch window so you're better than the looser with a debugger, someone else doesn't need colour coding - they actually know the syntax of the language unlike you (because anyone who needs syntax highlighting obviously doesn't know the syntax themselves) - so they're better than you.

The truely sad thing is that while I wish you were a troll, I really do believe that you think "colour coding and text editing" is all the tools a coder needs.


Tuesday, July 20, 2004

Indent your C code as you want or not at all, then
http://www.gnu.org/software/indent/indent.html
it in a way suitable for the project.

Fredrik Svensson
Wednesday, July 21, 2004

Use a code reformatter and fuggedaboutit.

Format the code according to a team or company standard before checking it in (preferably with a script that does format+checkin, so it becomes just as easy as a plain check-in), to maintain consistency and make diffs of versions simpler.  But whenever you check it out, reformat it to your own preferences before editing it.

And as a side note, tabs are evil.  The worst is when tabs are used sometimes and spaces sometimes, so the indentation only looks right when viewed in the same IDE or editor that the programmer used to write it.  Print it or view it in anything else, and the indentation becomes all screwed up.  Of course, a code reformatter solves that problem also.

T. Norman
Wednesday, July 21, 2004

BTW, since this subject is brought up, how about LEVEL of indentation? How many spaces (or tabs), I mean? I personally prefer just one space, and sometimes get complaints that it's "too few", and no less than two should be used. How about you?

Egor
Wednesday, July 21, 2004

One space is nearly invisible, I'd complain about that too. I use two spaces for XML and HTML, and four spaces for program code. Four spaces seems to have emerged as the common standard.

Chris Nahr
Wednesday, July 21, 2004

Programmers fighting over indentation should definitively program python

Ignore my ignorance
Wednesday, July 21, 2004

> 'smart' editors and IDEs are for poor programmers

Pussy. There's nothing wrong with flipping switches.


Wednesday, July 21, 2004

"Programmers fighting over indentation should definitively program python"

So they can move from mere fighting to nuclear war.

T. Norman
Wednesday, July 21, 2004

+++The truely sad thing is that while I wish you were a troll, I really do believe that you think "colour coding and text editing" is all the tools a coder needs. +++

They're all I've used for going on 5 years now.  I still manage somehow to be employed.

Dude, you need stronger tools.  That's fine.  Some of us don't need training wheels.  It's ok.

muppet
Wednesday, July 21, 2004

I also don't use an IDE, except for glue to keep the project together so I can compile and debug it easily while working on it. All my editing is done in vim, and I don't use any wizards or automatically generated code.

That works fine for me, I can still average about 200 debugged lines of code a day. (Actual lines of code counted by sloccount, not counting comments or blank lines.)

I used an IDE before, but I wasn't nearly as effective in the IDE because all the bells and whistles just got in the way. If that floats your boat, however, go ahead and use it. Different strokes and all that.

sid6581
Wednesday, July 21, 2004

"Most people's C programs should be indented six feet downward." --Blair P. Houghton

Mike Schiraldi
Wednesday, July 21, 2004

last post rocks. (hoho)

K+R weren't trying for clarity, they were trying to economize on punched cards.

aaah, tipping chads out of the trouser cuffs... (snore)

trollop
Wednesday, July 21, 2004

muppet, you may start to appreciate powerful tools when getting more work done in less time starts to affect your paycheck immediately and unconditionally. Though I doubt that with an attitude like that this day will ever come.

Egor
Wednesday, July 21, 2004

Egor -

It will never come because I no longer take jobs with corporations that push arbitrary, non-technical metrics on programmers (or anyone else).

How do you measure "amount of 'stuff' done" where you work?  Is it lines of code, goals, milestones?

If milestones/goals, how are they defined?  Do they remain constant?  How often are they reviewed?  Are they negotiable (ie, when you are asked to do off-project work mid-quarter)?

muppet
Wednesday, July 21, 2004

Ah! Indentation is absolutely worthless!

Watch and learn:

db.cursor *crs1 = new db.cursor( GET_PROC ); db.executor *exec1 = new db.executor( EXEC_PROC); proc.validator *val1 = new proc.validator( VAL_PROC ); log.logger *log1 = new log.logger( LOG_PROC ); order.locator *loc1 = new order.locator( LOC_PROC ); crs1->open(); exec1->open(); while (crs1->next()) { if (!val1->isidvalid(crs1->getid())) { log1->report(val1->geterror()); continue; } loc1->locate( crs1 ); if (!val1->isorderunique(loc1->getlinesfound())) { log1->report(val1->geterror()); continue; } if (!val1->isorderprocessed(crs1->order->getstatus())) { log1->report(val1->geterror()); continue; } if (!val1->isdelievered(crs1->order->getstatus())) { log1->report(val1->geterror()); continue; } if (!crs->getlinenumber()) { exec1->execute(UPDATE_NO_NR, crs); } else { exec1->execute(UPDATE_NR, crs); } } crs1->close(); exec1->close(); delete crs1; delete exec1; delete val1; delete log1; delete loc1;

Any developer who can't read/understand this and maintain it, retaining its superior style, deserves a disemboweling cutlass up his ignobile behind!!!


:))))))))))))

Paulo Caetano
Wednesday, July 21, 2004

Paulo,

There are two errors in your code. I take this as a proof
that indention is indeed important

Ignore my ignorance
Thursday, July 22, 2004

*  Recent Topics

*  Fog Creek Home