How to Start a Software Company 2.0

by Richard Rodger

       
 
Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!)

Do you indent your code for good or evil? Of all the horrors that afflict the pilgrim programmer, evil indentation is the most horrific. The horrifying lack thereof, the horrifying use of tabs, the horrifying concoction of tabs and spaces, the ... well, read on fellow coder, and face the horror yourself!

But first, ask yourself one question: why do we indent? why do we bother? What is it in our very nature that makes indentation the first principal of coding?

Source code is all about structured logic. When you make it easy to see the logic, then the program is easy to read. That's why we indent. The first, foremost and only reason: it makes code easier to read. All else follows.

That's why I really have to wonder when I see badly-indented code. Do these coders care nothing for the ethics of the profession? How can they so callously cast these cancerous texts upon us? Do they care nothing for themselves? Will they not have to maintain these corrugations, six months hence? Do they secretly enjoy all-nighters and death marches? Perhaps...

So, in the interests of the programmer-on-the-street, I hereby lay down the evils of indentation. A how NOT to do it guide, if you will. If you have never consciously considered these great questions before now, if you have simply followed the pathetic example of your peers, the perilous proclivities of your peripatetic tutors, or the prescriptions of your workplace, then think now, or forever hold yourself back. Indentation is a detail that has to sweat.

⋅ ⋅ ⋅      ⋅ ⋅ ⋅ 

Here then are the evils of indentation. There's a suitably evil number of them: four.

The First Evil: To Tabulate

for( fs = 0; fs < sake; fs++ ) {
        thisIsNuts();
        if( mindcapacity < nineitems ) {
                fire();
        }
}

Evil Typewriter Hey, you know what? Tabs were invented for typewriters. In the modern software development environment, the tab key stands for only one thing: move this line of code to the correct indentation. It does not mean: insert a tab character. Just because you have tabs set up to show as four spaces as a kludge, does not mean that that the rest of the programming world thinks this is a good idea.

Tab characters will show up differently all over the place. The whole point of indentation is lost if the indentation is not the same for all developers on the project. Think of the children! For goodness sake stick to spaces. Every time you hit tab your IDE should insert the correct number of spaces to bring your code to the current indentation column. And that's it. Nothing fancy. But now, guess what? It looks the same for everyone. Do you realise how many lives you just saved? How many premature heart attacks you prevented?

The Second Evil: To Make Too Much Space

public void removeContext() {
    if( spacedOut() ) {
        line >>> allthewayover;
        meaning = inview ? ok : emptyspace;
    }
}

Evil Space So you like to indent by four spaces. Sure. Are you nuts? what a waste of screen real-estate. One of the biggest aids to program comprehension is ... code on the screen. Yup, the more code you can see the better. Get a bigger monitor and see the difference. So with your IDE of choice squashing the code window from all sides, you really need to avoid pushing code off to the right. It just disappears out of sight and out of mind.

So keep it to two spaces. Always and forever. There is no other way to optimise this. One space is not clear enough. Three spaces is bizarre. Four is wasteful. Anything more, like say, eight (Some people actually do this - God help us all), and you should be fired for wasting bytes.

OK, I'll relax a bit. Sometimes you can use four spaces to indent. When there is a substantial change of context, for example, anonymous inner classes (they are freaky enough to warrant their own rule, don't you think?), then it makes sense. It's a good visual clue that something a bit different is going on.

The Third Evil: To Stray From The Path

if( logic ) {
    canbefollowed();
  } else if( logic ) {
isclear();
} else {
    runaway();
}
  crying();

Evil Path So you've changed the settings on your IDE, you use two spaces, tab inserts two spaces, and auto-indents to the correct level, and you're sitting pretty. Not so fast, flyboy.

You are indenting everywhere right? And I mean, everywhere. There is no point doing it in a few places and getting lazy when you do your usual cut-and-paste coding anti-pattern. Or you make some changes to code that was written by someone else and you just, ignore the indentation. Sure it's only here and there. Bzzzt! can you say spaghetti code?

Stick to the one true path: indent everything, and never mix tabs and spaces.

A foolish consistency may be the hobgoblin of little minds, but you're not writing poetry, you're writing code. Get a small mind. A really really teeny-weeny tiny one. Get obnoxious about consistency and the coding brethren (that's a gender-neutral term, by the way) will thank you! Come on, neat code just makes your day.

The Fourth Evil: To Break The Bond

public void myProfessor()
  {
    toldMe();
    if( makeCodeReallyReallyClear() ) 
      {
        thenIAmGood();
      }
    else
      {
        actuallyIAmWasteOfSpace();
      }
  } 

A Good Bond So this one is a bit of thrown gauntlet. Here's the question: what does each level of indentation signify? Well? It's not hard...

One level of logic. Just one. Not two. One. Each level of indentation says we've gone a one level deeper into the decision structure. There is an intrinsic bond between indent and logic. Keep to this rule and your eye can scan the code and see that it is good. You can pick out the flow of the code from 300 yards and hit the target with your back turned using a mirror.

So why, oh why, do some people find it necessary to indent two levels, wasting a line into the bargain? Not a chance you can justify this. Maybe someone took a beginners design course where they were told that whitespace was good. Sure it is. But, you know, all things in moderation...

⋅ ⋅ ⋅      ⋅ ⋅ ⋅ 

Come on people! There are no excuses. Most source editors will auto-indent for you anyway. Let's get with the program and remove this root of programmer pain. We have enough to worry about with demanding managers, impossible deadlines and long hours. Let's just do each other a favour.

In case you think I'm all talk and no action, here's the action. You can scour my code for indentation evils. I have an open source project, Jostraca, that's been around for about five years and has had plenty of time to build up a bit of cruft in the codebase. So get going! Everybody who finds a violation will get a FREE CSV Manager single developer license, worth $97. Oh yes, that's real money.

Here are the rules: you have to be the first to find and post the example of incorrect indentation in the comments to this entry. You have to state which evil is being perpetrated. If you're second fiddle, tough luck. The example has to be taken from the latest release (1.4.0). Generated code and non-Java code don't count so don't bother looking there. Oh, and I reserve the right to invent new rules if I feel like it.

So come on, are you gonna bankrupt me for mouthing off or what?

@ 04:07 PM GMT+00:00 [ comments [6] ]   email this   links to this

If you liked this entry, please consider bookmarking it &mdash Thanks!
Bookmark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at del.icio.us Digg Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at Digg.com Bookmark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at reddit.com Bookmark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at YahooMyWeb Bookmark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at Spurl.net Bookmark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at Simpy.com Bookmark Polyphasic Mutants at NewsVine Blink this Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at blinklist.com Bookmark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at Furl.net Fark Rant: The Evils of Indentation (FREE STUFF If You Prove Me Wrong!) at Fark.com

 
 
Trackback URL: http://www.richardrodger.com/roller/trackback/richard/Weblog/rant_the_evils_of_indentation
Comments:

2 spaces?! Yukky!
I'd rather use Xinerama!

Posted by Eoin on December 06, 2005 at 04:49 PM GMT+00:00 #

But Mr. Project Manager, Sir, my coding-style *requires* me to use two monitors...

Posted by Richard Rodger on December 06, 2005 at 04:55 PM GMT+00:00
Website: http://www.ricebridge.com #

The Linux kernel coding standard requires 8-character indentation:

"Tabs are 8 characters, and thus indentations are also 8 characters. 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.

"Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

"Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

"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."


Personally, I think 8-char indents are overkill, and 4 is just right. 2, on the other hand, is just not enough. I've tried 2-char indenting with Ruby (it's the standard there), and different levels of indentation start to blur together after a long day of coding.

Posted by Charles Miller on December 07, 2005 at 02:48 AM GMT+00:00
Website: http://fishbowl.pastiche.org #

One issue with 2-char indenting: it encourages lots of levels of idententation. This in itself has its own sets of problems.

The fifth evil of indentation is to have 10 levels in the single method.

Posted by Robert Watkins on December 07, 2005 at 04:50 AM GMT+00:00
Website: http://twasink.net #

Or you could just use tabs for indentation and then set your tab size to whatever you want. The author of this blog can set the tab size to 2, I can set it to 4 (my preferred size) and the Linux kernel guys can set it to 8.... Then the code is perfectly indented for each person... so what exactly is wrong with tabs again? Just thing of tab as an alias for "one level of indentation".

Posted by John Evans on December 07, 2005 at 05:14 AM GMT+00:00
Website: http://www.jpevans.com/ #

Charles: the only argument is that it's easier to read after 20 hours of coding. Hmm, maybe they should take a break sometime and go to the Pub or something. Anyway, just because Linus does something doesn't mean it's kosher - he didn't use source control for years.

I agree with the statement that you should keep your indentation levels down. The problem with Java is that is is a very verbose language and any given line of code is bound to be a lot longer than a line of C code, so you hit the right edge a lot sooner.

Robert: argh!, yeah, forgot that one. I don't think that five is such an evil number though, so I'm kind of stuck there. Maybe I'll do a Stephen Covey job on it.

John: That's a failed dream. In every company/project where I've seen this policy, some nitwit goes and uses spaces. And then you can't *see* the problem, so it festers.

It's a fragile solution that relies on people to act like machines. They never do.

So the big issue is that 2 space indents are unclear. But wait a sec, I think that's only an issue if you have *too* many levels of indentation. The 2 space rule is optimised for good code. The fact that deeply nested code is harder to read with a 2 space indent is a code smell.

Posted by Richard Rodger on December 07, 2005 at 03:08 PM GMT+00:00
Website: http://www.ricebridge.com #

Comments for this have been disabled. Please send me a mail if you want to comment and I will activate comments again.
 
YahooBloglines
NewsgatorMSN
Google Readerdel.icio.us FurlSubscribe with myFeedster
« December 2005 »
SunMonTueWedThuFriSat
    
3
5
10
11
13
17
18
24
25
26
27
28
29
30
31
       
Today

All | General | Java | Business | Fun | Perl | Rant | Ireland | Web
[This is a Roller site]
[Valid Atom 1.0] [Valid RSS]
Technology Blog Top Sites
Blogarama - The Blogs Directory

Blog Directory & Search engine

Blog Flux Directory
Irish Blogs
 View My Public Stats on MyBlogLog.com

Performancing
Enter your Email


Powered by FeedBlitz
Theme adapted from Sotto.
 
Ricebridge XML Manager
  • Convert XML to a table of data
  • Convert XML to CSV, and CSV to XML
  • High-speed, single-pass XPath
  • Memory-stable and fault-tolerant
  • Loads of documentation
  • Cut-and-paste code examples
  • Find a bug, get a gift cert
Ricebridge Java XML Manager Component


Ricebridge CSV Manager
  • Convert CSV to a table of data
  • Handle any type of delimited file
  • Memory-stable and fault-tolerant
  • Loads of documentation
  • Cut-and-paste code examples
  • Find a bug, get a gift cert
Ricebridge Java CSV Manager Component


Popular Posts

 Sign up for MyBlogLog.com
Alertra Website Monitoring Service
Get Chitika eMiniMalls
Solo Tees
BlogJet