Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Been writing code since about 18 years now. Lately, I've got into the habit of not indenting elses and when I look at the code it does worry me a bit mostly that someone who might look at the code might cry foul.

The snippet below explains the need for the style

if(condition) {
   ...
} //1 
if(condition) { // Don't want this second block to be entered unnecessarily
   ...
}

So I put an else where the comment labelled 1 is

if(condition) {
   ...
} else
if(condition) { 
   ...
}

Putting an else there helps maintain readability especially when forgetting to put one there doesn't necessarily break anything. What I'm uneasy with is that it doesn't strictly follow conventions (Sun's in this case) in two different ways - 1) proper indentation 2) having an open brace after the else

I guess what I'm trying to do is prevent having massive indents which really makes a difference in blocks that have a lot of conditions - 5 or more although only three are showcased below.

if(condition) {
   ...
} else {
   if(condition) { 
      ...
   } else {
      if(condition) { 
         ...
      } else {
         ...
      }
   }
}

I find it a lot neater to have the else unindented and without a brace following. What are your thoughts?

share|improve this question
1  
that was a mistype, thanks for pointing it out - corrected –  sfk Feb 21 '13 at 11:52
1  
Failing to indent else's is sloppy, bad style. Period. –  user61852 Feb 21 '13 at 11:56
    
@user61852 hence the question –  sfk Feb 21 '13 at 11:57
    
I've been coding forever too, and that looks seriously misleading. You're much better off with the answer from @JoachimSauer below. –  Ross Patterson Feb 21 '13 at 12:13
3  
You're just masking the arrowhead anti-pattern, not solving it. This would be worse imo. If the code smells I want to see the problem right away... –  MattDavey Feb 21 '13 at 12:34

1 Answer 1

up vote 15 down vote accepted

I usually see this solved by not putting the if inside a code block if it's the only statement in the else-part and putting the if one the same line.

This effectively emulates the elseif or elif of other languages.

if (condition1) {
  ...
} else if (condition2) {
  ...
} else {
  ...
}

This way there's no useless indentation and every code block has its braces, as it should be.

I also never had any code quality tool nag at me for this, so I assume it's widely accepted.

share|improve this answer
    
I think he's familiar with this style. The point was that he doesn't want condition2 too far on the right-hand side for the sake of readability. –  Neil Feb 21 '13 at 11:43
2  
It's actually explicitly listed in Oracle's coding conventions (can't imagine they changed this from Sun's conventions): oracle.com/technetwork/java/javase/documentation/… –  Baqueta Feb 21 '13 at 11:44
    
@Neil: that's not how I read it. The "massiv identation" I read in the question, seems to be from the later style mentioned in the post, where the 2nd, 3rd, ... condition gets indented even further than. –  Joachim Sauer Feb 21 '13 at 11:44
    
I did like the bit where code quality tools don't nag even though the brace after the else is missed; @Baqueta yes I was trying to event condition2 being indented; nice to see it part of sun's/oracle's convention –  sfk Feb 21 '13 at 11:49
    
@JoachimSauer, well if that's true, someone needs to tell him sooner or later. –  Neil Feb 21 '13 at 12:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.