Omitting the break; in the default: section of a switch()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Lucas-Smith

    Omitting the break; in the default: section of a switch()



    Is there any need to keep the final break in a switch which uses a default
    at the end? I.e:


    switch ($data) {

    case 'foo':
    # Action
    break;

    case 'bar':
    # Action
    break;

    default:
    # Action
    break; # Is there any reason to keep this line?
    }



    I'm trying to aim for best practice levels of coding. (e.g. using ''
    rather than "", etc. etc).

    As far as I can see there seems no reason to do so and it improves
    readability of code through and is (ultra-marginally) more efficient.



    Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22


    Senior Computing Technician (Web Technician)
    Department of Geography, University of Cambridge (01223 3)33390

    & Webmaster, SPRI
    Scott Polar Research Institute, University of Cambridge


  • P'tit Marcel

    #2
    Re: Omitting the break; in the default: section of a switch()

    Martin Lucas-Smith écrivit:
    [color=blue]
    >
    >
    > Is there any need to keep the final break in a switch which uses a
    > default at the end?[/color]

    Of course not. breaks are even not mandatory in the non-final cases. It
    just depends on what you are aiming to.


    --
    P'tit Marcel

    Comment

    • Martin Lucas-Smith

      #3
      Re: Omitting the break; in the default: section of a switch()


      [color=blue][color=green]
      > > Is there any need to keep the final break in a switch which uses a
      > > default at the end?[/color]
      >
      > Of course not. breaks are even not mandatory in the non-final cases. It
      > just depends on what you are aiming to.[/color]

      I realise that breaks are not mandatory (though if not using them that
      should be documented), but it seems to be me that there are never any
      circumstances where putting a break in the default: section can make any
      difference. Would others agree?

      After all, the default is the last in the list, so the break automatically
      happens if it's got that far down.


      Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22


      Senior Computing Technician (Web Technician)
      Department of Geography, University of Cambridge (01223 3)33390

      & Webmaster, SPRI
      Scott Polar Research Institute, University of Cambridge


      Comment

      • Jon Kraft

        #4
        Re: Omitting the break; in the default: section of a switch()

        Martin Lucas-Smith <mvl22@cam.ac.u k> wrote:
        [color=blue]
        > Is there any need to keep the final break in a switch which uses a
        > default at the end? I.e:
        >
        >
        > switch ($data) {
        >
        > case 'foo':
        > # Action
        > break;
        >
        > case 'bar':
        > # Action
        > break;
        >
        > default:
        > # Action
        > break; # Is there any reason to keep this line?
        > }[/color]

        No reason to have it in "default" whatsoever.

        See example 4


        JOn

        Comment

        • Jeffrey Silverman

          #5
          Re: Omitting the break; in the default: section of a switch()

          On Fri, 18 Jul 2003 13:17:47 +0000, Jon Kraft wrote:
          [color=blue]
          > Martin Lucas-Smith <mvl22@cam.ac.u k> wrote:
          >[color=green]
          >> Is there any need to keep the final break in a switch which uses a
          >> default at the end? I.e:
          >>
          >>
          >> switch ($data) {
          >>
          >> case 'foo':
          >> # Action
          >> break;
          >>
          >> case 'bar':
          >> # Action
          >> break;
          >>
          >> default:
          >> # Action
          >> break; # Is there any reason to keep this line?
          >> }
          >> }[/color]
          > No reason to have it in "default" whatsoever.
          >
          > See example 4
          > http://uk.php.net/manual/en/control-...res.switch.php
          >
          > JOn[/color]

          Not entirely true. (But it is true *in this case* (no pun intended))

          The default does not have to be the last item! In such a situation you may
          want to have a break.

          for example...

          switch ($foo) {
          case "bar":
          default:
          echo "Your momma";
          break;
          case "bing":
          echo "I like cheese";
          break;
          }

          Both the default and case "bar" take the same action and there is a break
          after default. Also, default is first.
          --
          Jeffrey D. Silverman | jeffrey AT jhu DOT edu
          Johns Hopkins University | Baltimore, MD
          Website | http://www.wse.jhu.edu/newtnotes/

          Comment

          • Default User

            #6
            Re: Omitting the break; in the default: section of a switch()



            Martin Lucas-Smith wrote:[color=blue]
            >
            > Is there any need to keep the final break in a switch which uses a default
            > at the end? I.e:
            >
            > switch ($data) {
            >
            > case 'foo':
            > # Action
            > break;
            >
            > case 'bar':
            > # Action
            > break;
            >
            > default:
            > # Action
            > break; # Is there any reason to keep this line?
            > }
            >
            > I'm trying to aim for best practice levels of coding. (e.g. using ''
            > rather than "", etc. etc).
            >
            > As far as I can see there seems no reason to do so and it improves
            > readability of code through and is (ultra-marginally) more efficient.[/color]


            There's no programmatic reason for it. It is sometimes recommended as a
            maintenance feature, so that if for some reason the cases are reordered
            it won't be mistakenly left out. While it's common to have the default
            case come last in the sequence, there isn't any requirement for it
            either.



            Brian Rodenborn

            Comment

            Working...