The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Indentation preference in a SELECT/CASE statement.

This is such a small point, but I have a curiosity question about indentation preferences regarding the SELECT/CASE statement.

While I might write a SELECT/CASE statement like:

  Select Case inputChar
  Case "a" to "z"
    charType = LETTER
  Case "0" to "9"
    charType = DIGIT
  Case Else
    charType = INVALID
  End Select

I've noticed that some people prefer an extra level of indentation:

  Select Case inputChar
    Case "a" to "z"
      charType = LETTER
    Case "0" to "9"
      charType = DIGIT
    Case Else
    charType = INVALID
  End Select

What I don't understand is that while the following is functionally equivalent:

  If inputChar >= "a" and inputChar <= "z" Then
    charType = LETTER
  ElseIf inputChar >= "0" and inputChar <= "9" Then
    charType = DIGIT
  Else
    charType = INVALID
  End If

I've never seen anyone write:

  If inputChar >= "a" and inputChar <= "z" Then
      charType = LETTER
    ElseIf inputChar >= "0" and inputChar <= "9" Then
      charType = DIGIT
    Else
      charType = INVALID
  End If

The CASE and ELSEIF clauses serve the same purpose at the same semantic level, yet the CASE clause is sometimes indented more than the ELSEIF.  Why is the extra level of indentation sometimes prefered in a SELECT/CASE statement but "never" seen in an IF/ELSEIF statement?  Perhaps I'm mistaken about the CASE and ELSEIF being at the same semantic level?  Or, perhaps I just haven't seen the work of those who indent ELSEIF clauses.

For those who are morbidly curious as to why I would ask such a silly little question...  I was reading Code Complete 2 (Steve McConnell's latest work) and came across an example of a SELECT/CASE statement that had the extra indentation (bottom of page 360).  So, I decided I'd get a few opinions on the possible reasons for this particular preference.
bpd
Wednesday, September 15, 2004
 
 
SELECT CASE does something different than the contained CASE statements while each IF and ELSIF are essentially similar constructs...
Kenny Send private email
Wednesday, September 15, 2004
 
 
In C/C++ it seems pretty common to do this:

select (var) {
case 1:
  blah;
  break;
case 2:
  blah;
  break;
}

I don't see too much of this:

select (var) {
  case 1:
    blah;
    break;
  case 2:
    blah;
    break;
}
Almost Anonymous Send private email
Wednesday, September 15, 2004
 
 
AA: I don't see much of that either, although the corresponding form is common in shell scripts. I write switch statements the way you do in C and C++, but I do introduce the extra level of indentation when writing them in VB. The reason is that C switch and B select case are structurally different because of fallthrough. C case labels are just that: labels. The VB case statements introduce blocks, and I always indent blocks.

Having said that, I have a hard time getting excited about this sort of issue. I wouldn't criticize either form as long as it's used consistently.
comp.lang.c refugee
Wednesday, September 15, 2004
 
 
Am I the only one who does:

select (var)
{
case 1:
  {
    blah;
  }
  break;

case 2:
  {
    blah;
  }
  break;
}

?

(BTW, this is a silly topic - write them however you or your coding standard want.)

Thursday, September 16, 2004
 
 
Given the way that white space and tabbing is handled here its entirely pointless to post questions about tab/spacing style in code.

Whether this was intentional so as not to have such threads I'll leave to the imagination of the reader.
Simon Lucy Send private email
Thursday, September 16, 2004
 
 
It's not intended, it's a bug :^D
buggy
Thursday, September 16, 2004
 
 
To poster "" above - no, I do that more or less

select(monkey)
{
  case 1 :
  {
    // multiline stuff
  } break;

  case 2 :
  {
  } break;
}

Indention to me represents logical grouping - the case statements are 'inside' the select statement and thus should be indendented.
Mr Jack
Friday, September 17, 2004
 
 
Yes the second is a bit silly. You get the same in C-style languages with switch

switch (foo) {
case 1:
  blah;
  break;
case 2:
  blah;
  break;
}

one indent is quite enough, ultimately though who cares? just do what you think is right.
Matt Send private email
Sunday, September 26, 2004
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz