Thread: switch indentation styles

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    switch indentation styles

    What's the best way to indent a switch?

    Most people use this:
    Code:
    switch(var) {
        default:
            somefunc();
            break;
    }
    But to me that looks like it's indented twice, sort of like this:
    Code:
    if(var)
        {
            somefunc();
        }
    Some people use this:
    Code:
    switch(var) {
    default:
        somefunc();
        break;
    }
    But that looks like it wasn't indented at all.

    I like this style (although it looks indented twice, too):
    Code:
    switch(var) {
        default: {
            somefunc();
        } break;
    }
    But a book I read suggested that "case 0: {" was illegal. Is it?

    Are there any other ways to indent a switch?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is related to general bracing style, but my preference (which was sorta thrown at me by my beautifier at first) is the second.
    Code:
          switch ( ch )
          {
          case 'L': 
             /* ... */
             break;
          case 'D': 
             /* ... */
             break;
          case 'Q': 
             /* ... */
             break;
          }
    I began to prefer it after having seen switches nested more than 3 deep -- which meant indentation at more than 6 levels.
    Last edited by Dave_Sinkula; 09-17-2005 at 05:30 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, thanks. But is "case 0: {" illegal?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    More context?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, my book said "Try not to put braces after the cases" or something. So I took it to mean that
    Code:
    switch(var) {
        default: {
            somefunc();
        } break;
    }
    was illegal.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    No, brace-enclosed code blocks are not illegal. In a switch statement they are often used to declare variables local only to that case.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think the book meant that a goto label (ie, case) could not precede a block.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Then you wouldn't like to have seen where I used such a thing in production code. (Commented of course!)

    [edit]It was something truly hideous, something like this.
    Code:
          switch ( x )
          {
          case 0: 
             /* ... */
             if ( y )
             {
          case 1: 
                /* ... */
             }
             break;
          }
    But it did what I wanted, and it did it how I wanted it done.
    Last edited by Dave_Sinkula; 09-17-2005 at 06:29 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    case 0: { /* code */ } break;
    Is legal

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well then, I guess the book's wrong. I'll post its title once I find it again. It was a Dummies book.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thats not surprising.

    heck you can even do:
    Code:
    case 0: { /* Block 1 */ } { /* Block 2 */ } { /* Block 3 */ { /* Block 4 in Block 3 */ } } break;
    Though if you did that the ninjas would have to come and break your hands.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I prefer:
    Code:
    switch( foo )
    {
        case bar:
            baz( );
        break;
    
        ...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I prefer:
    Code:
    if(foo==bar)
    {
        foobar();
    }
    but if I have to use a switch (yes, there are cases where it's worth it, like days in a month)
    Code:
    switch(foo)
    {
        case bar:
        {
            foobar();
            break;
        }
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I prefer
    Code:
    switch (expr) {
    case x:
    	{
    	/* blah */
    	}
    	break;
    case y:
    	/* blah blah */
    	break;
    default:
    	/* gah */
    }
    But that's mainly because I want to keep the statements in the switch on the same level of indentation if they were in if-else structures instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Visual C++ 6.0 for Dummies, Quick Reference.

    I like their sample program (page 132):

    Code:
    main(int argc, char *argv[]) {
    char UserInput[_MAX_PATH];
        for(;;;) {
            cout << "Your input? "    // not a typo
            cin >> UserInput   // nor here
            if(!strcmp(UserInput, "bye")) break;
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 11:17 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM