Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Switch boolean.... (reinventing if, unnecessarily)

Switch boolean.... (reinventing if, unnecessarily)

Scheduled Pinned Locked Moved The Weird and The Wonderful
47 Posts 32 Posters 33 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rob Grainger

    Came across this kind of code today...

    void EnableFromValue(bool enabled)
    {
    switch (enabled) {
    case true:
    FirstControl.Enabled = true;
    SecondControl.Enabled = true;
    ...
    break;
    case false:
    FirstControl.Enabled = false;
    SecondControl.Enabled = false;
    ...
    break;
    }
    }

    I'm sure there must be a better way ;-)

    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

    A Offline
    A Offline
    Al Chak
    wrote on last edited by
    #36

    If you are sure that the function argument named enabled is bool forever? If yes than there is no reason for the switch. If chacne to change the argument type is exist so - I would add into the switch

    default:
    FirstControl.Enabled = enabled;
    SecondControl.Enabled = enabled; // :laugh:

    Richard DeemingR 1 Reply Last reply
    0
    • S Scott Corbett

      Umm...that's not true. A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.) On the other hand, if/else statements do bail as soon as the first passing conditional is found and the associated code block is executed.

      Scott E. Corbett

      S Offline
      S Offline
      Simon ORiordan from UK
      wrote on last edited by
      #37

      Well I kind of assumed you knew how to write a switch.

      1 Reply Last reply
      0
      • A Al Chak

        If you are sure that the function argument named enabled is bool forever? If yes than there is no reason for the switch. If chacne to change the argument type is exist so - I would add into the switch

        default:
        FirstControl.Enabled = enabled;
        SecondControl.Enabled = enabled; // :laugh:

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #38

        Al Chak wrote:

        If chacne to change the argument type is exist so - I would add into the switch

        There's no reason to add a switch statement now just in case the argument type changes in six months. Add the switch when you need it - ie: when the argument type changes. :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Scott Corbett wrote:

          A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.)

          Not in C# - every case is required to have a terminating statement (break, goto, return or throw).

          switch (C# Reference)[^]:

          Unlike C++, C# does not allow execution to continue from one switch section to the next. ... C# requires the end of switch sections, including the final one, to be unreachable. That is, unlike some other languages, your code may not fall through into the next switch section.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          S Offline
          S Offline
          Scott Corbett
          wrote on last edited by
          #39

          You're right about the switch case and fall through. Been spending too much time playing with C++ lately. My apologies.

          Scott E. Corbett

          1 Reply Last reply
          0
          • R Rob Grainger

            Came across this kind of code today...

            void EnableFromValue(bool enabled)
            {
            switch (enabled) {
            case true:
            FirstControl.Enabled = true;
            SecondControl.Enabled = true;
            ...
            break;
            case false:
            FirstControl.Enabled = false;
            SecondControl.Enabled = false;
            ...
            break;
            }
            }

            I'm sure there must be a better way ;-)

            "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

            C Offline
            C Offline
            chaosworrier_oz
            wrote on last edited by
            #40

            If it is not break-en, don't switch it? [[Thank you, I'm here all week; try the veal...]] Chaos.

            B 1 Reply Last reply
            0
            • R Rob Grainger

              Came across this kind of code today...

              void EnableFromValue(bool enabled)
              {
              switch (enabled) {
              case true:
              FirstControl.Enabled = true;
              SecondControl.Enabled = true;
              ...
              break;
              case false:
              FirstControl.Enabled = false;
              SecondControl.Enabled = false;
              ...
              break;
              }
              }

              I'm sure there must be a better way ;-)

              "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #41

              well, switch is shorthand for the if--then--else construct.

              #SupportHeForShe

              If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

              1 Reply Last reply
              0
              • J jibalt

                So do other things in the switch, but set the common values just once outside the switch ... duh. It's the DRY principle, and duplicating the code in each branch of the switch is not only stupid, but error prone.

                T Offline
                T Offline
                TheGreatAndPowerfulOz
                wrote on last edited by
                #42

                :thumbsup:

                #SupportHeForShe

                If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                1 Reply Last reply
                0
                • F Freak30

                  I see a reason for the function but not for the switch statement. Except if you are paid by lines of code of course. :-D

                  The good thing about pessimism is, that you are always either right or pleasently surprised.

                  D Offline
                  D Offline
                  DSewhuk
                  wrote on last edited by
                  #43

                  If they had a default: case then I could almost see a reason. Consider the case if the bool was neither true or false. For most x86 C's there are 254 other trues and 65534 other trues on the DSP I code. I remember some very picky standards about what to do with unexpected values for space computing. Pesky alpha/beta/gamma particles flipping RAM cells around and the like. I have fixed my share of mixed boolean true logics gone bad. Is it a one, -1 or non-zero? If true == mySupposedBool can be very tricky to find in C when mySupposedBool = -1 from some other language interface. At least false seems to always == 0. ----- I love standards, there is so many to choose from!

                  D 1 Reply Last reply
                  0
                  • D DSewhuk

                    If they had a default: case then I could almost see a reason. Consider the case if the bool was neither true or false. For most x86 C's there are 254 other trues and 65534 other trues on the DSP I code. I remember some very picky standards about what to do with unexpected values for space computing. Pesky alpha/beta/gamma particles flipping RAM cells around and the like. I have fixed my share of mixed boolean true logics gone bad. Is it a one, -1 or non-zero? If true == mySupposedBool can be very tricky to find in C when mySupposedBool = -1 from some other language interface. At least false seems to always == 0. ----- I love standards, there is so many to choose from!

                    D Offline
                    D Offline
                    Daniel Pfeffer
                    wrote on last edited by
                    #44

                    DSewhuk wrote:

                    If they had a default: case then I could almost see a reason. Consider the case if the bool was neither true or false. For most x86 C's there are 254 other trues and 65534 other trues on the DSP I code. I remember some very picky standards about what to do with unexpected values for space computing. Pesky alpha/beta/gamma particles flipping RAM cells around and the like.

                    Interesting; most programmers never have to consider the possibility of failure of the CPU/memory in their code. Does this imply that you must use a form of trinary logic (true / false / bad value) in such code?

                    DSewhuk wrote:

                    I have fixed my share of mixed boolean true logics gone bad. Is it a one, -1 or non-zero?

                    My problem is - how many compilers assume that a Boolean has only 'true' or 'false' values, ignoring the 'default' clause in this case? One solution would be to have your interface code treat 'Boolean' values as appropriate-size integers, converting to an appropriate type (e.g. true / false / bad value). This leads us to the logic above, where Boolean values are not truly Boolean. :)

                    If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                    1 Reply Last reply
                    0
                    • R Rob Grainger

                      Came across this kind of code today...

                      void EnableFromValue(bool enabled)
                      {
                      switch (enabled) {
                      case true:
                      FirstControl.Enabled = true;
                      SecondControl.Enabled = true;
                      ...
                      break;
                      case false:
                      FirstControl.Enabled = false;
                      SecondControl.Enabled = false;
                      ...
                      break;
                      }
                      }

                      I'm sure there must be a better way ;-)

                      "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #45

                      They forgot the default clause! :wtf: :laugh:

                      -- Kein Mitleid Für Die Mehrheit

                      1 Reply Last reply
                      0
                      • C chaosworrier_oz

                        If it is not break-en, don't switch it? [[Thank you, I'm here all week; try the veal...]] Chaos.

                        B Offline
                        B Offline
                        Bernhard Hiller
                        wrote on last edited by
                        #46

                        If you use "simple" booleans, that may be unnecessary. But what about "extended" booleans (i.e. true, false, don_t_know, FILE_NOT_FOUND)? That's where a switch statement comes in very handy.

                        1 Reply Last reply
                        0
                        • R Rob Grainger

                          Came across this kind of code today...

                          void EnableFromValue(bool enabled)
                          {
                          switch (enabled) {
                          case true:
                          FirstControl.Enabled = true;
                          SecondControl.Enabled = true;
                          ...
                          break;
                          case false:
                          FirstControl.Enabled = false;
                          SecondControl.Enabled = false;
                          ...
                          break;
                          }
                          }

                          I'm sure there must be a better way ;-)

                          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                          B Offline
                          B Offline
                          Bernhard Hiller
                          wrote on last edited by
                          #47

                          With "simple" booleans, a switch statement is not necessary. But what about "extended" boolenas (i.e. true, false, don_t_know, FILE_NOT_FOUND)? That's where a switch comes in very handy.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups