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 Insider News
  4. Why you should never use a boolean field (use an Enum instead)

Why you should never use a boolean field (use an Enum instead)

Scheduled Pinned Locked Moved The Insider News
csharpdatabase
25 Posts 11 Posters 1 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.
  • K Kevin Marois

    I disagree. The problem he described in the article is not the choice of data type to use, but rather simply a lack of requirements analysis.

    If it's not broken, fix it until it is

    C Offline
    C Offline
    crazedDotNetDev
    wrote on last edited by
    #10

    Absolutely… use the right tool for the right job.

    1 Reply Last reply
    0
    • T Terrence Dorsey

      CodeClimber[^]:

      When I tweeted about this while I was refactoring, someone told me: “Using a boolean is an antipattern”… well… now I experienced it myself. From this experience, in the future, I’ll never use a Boolean field again, and always start with an Enum, especially with a Document database where migrating data is a bit more complex than with relational databases.

      State management is not ideal for a boolean value, but booleans have value.

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #11

      The problem here is with this: I needed to keep track of whether someone has paid or not, so I started modeling that field as a simple boolean value, HasPaid. He should have realized from the get-go, by simply thinking of use-cases in his head, that a boolean would not be appropriate. But so often, I find that people don't think of use-cases as their coding, and then they blame someone / something else for the eventual problem that they get into. And hence we have refactoring, which is another word for "Doh!" The problem isn't with booleans, it's with the programmer! Marc

      Reverse Engineering Legacy Applications
      How To Think Like a Functional Programmer
      My Blog
      Computational Types in C# and F#

      1 Reply Last reply
      0
      • P Pete OHanlon

        I have seen the light. I have been wrong, all this time. From now on, all the code I write is going to contain this enumeration:

        public enum LogicalStatus
        {
        True,
        False
        }

        Now, all my code will be beautiful. I am assured that I will be able to ascend to a higher level of consciousness now.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #12

        Pete O'Hanlon wrote:

        Now, all my code will be beautiful.

        And I will know who to blame when I see this in some code sometime in the future. "Oh, I read this on the Code Project by a reputable member and have been programming like this ever since. In fact, I've extended the pattern:

        public enum Integers
        {
        One,
        Two,
        Three,
        Four,
        }

        but for some strange reason, I have to keep adding new Integers! Plz Help. Urgentz!" ;) Marc

        Reverse Engineering Legacy Applications
        How To Think Like a Functional Programmer
        My Blog
        Computational Types in C# and F#

        P 1 Reply Last reply
        0
        • M Marc Clifton

          Pete O'Hanlon wrote:

          Now, all my code will be beautiful.

          And I will know who to blame when I see this in some code sometime in the future. "Oh, I read this on the Code Project by a reputable member and have been programming like this ever since. In fact, I've extended the pattern:

          public enum Integers
          {
          One,
          Two,
          Three,
          Four,
          }

          but for some strange reason, I have to keep adding new Integers! Plz Help. Urgentz!" ;) Marc

          Reverse Engineering Legacy Applications
          How To Think Like a Functional Programmer
          My Blog
          Computational Types in C# and F#

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #13

          You fool. You've prematurely optimized. Don't you know that you should be using the Visitor pattern here, possibly combined with DI to inject new numbers as and when you see fit? Of course, I expect to see a full implementation for Doubles and Floats while you are at it (and just to be on the safe side, I think you need to represent all possible combinations of strings).

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          W 1 Reply Last reply
          0
          • P Pete OHanlon

            I have seen the light. I have been wrong, all this time. From now on, all the code I write is going to contain this enumeration:

            public enum LogicalStatus
            {
            True,
            False
            }

            Now, all my code will be beautiful. I am assured that I will be able to ascend to a higher level of consciousness now.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            W Offline
            W Offline
            wout de zeeuw
            wrote on last edited by
            #14

            FTFY:

            [Flags]
            public enum LogicalStatus : byte {
            False = 0,
            True = 1
            }

            Wout

            1 Reply Last reply
            0
            • P Pete OHanlon

              You fool. You've prematurely optimized. Don't you know that you should be using the Visitor pattern here, possibly combined with DI to inject new numbers as and when you see fit? Of course, I expect to see a full implementation for Doubles and Floats while you are at it (and just to be on the safe side, I think you need to represent all possible combinations of strings).

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              W Offline
              W Offline
              wout de zeeuw
              wrote on last edited by
              #15

              And add a snuff of IoC, so you can switch it out at runtime with some 3rd party library providing the implementation!

              Wout

              1 Reply Last reply
              0
              • P Pete OHanlon

                I have seen the light. I have been wrong, all this time. From now on, all the code I write is going to contain this enumeration:

                public enum LogicalStatus
                {
                True,
                False
                }

                Now, all my code will be beautiful. I am assured that I will be able to ascend to a higher level of consciousness now.

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #16

                Hey Pete, I was wanting to poke around CodeStash, but I'm getting: "Google Chrome could not connect to www.codestash.co.uk" Is the site still up? Marc

                Reverse Engineering Legacy Applications
                How To Think Like a Functional Programmer
                My Blog
                Computational Types in C# and F#

                P 1 Reply Last reply
                0
                • M Marc Clifton

                  Hey Pete, I was wanting to poke around CodeStash, but I'm getting: "Google Chrome could not connect to www.codestash.co.uk" Is the site still up? Marc

                  Reverse Engineering Legacy Applications
                  How To Think Like a Functional Programmer
                  My Blog
                  Computational Types in C# and F#

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #17

                  Hi Marc, I've just had to ask the CodeProject team to restart the server as it's hosted on their servers. Regards Pete

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                  M 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Hi Marc, I've just had to ask the CodeProject team to restart the server as it's hosted on their servers. Regards Pete

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #18

                    Pete O'Hanlon wrote:

                    I've just had to ask the CodeProject team to restart the server as it's hosted on their servers.

                    Hmmm, it's still not responding. I guess the hamsters are on vacation. Marc

                    Reverse Engineering Legacy Applications
                    How To Think Like a Functional Programmer
                    My Blog
                    Computational Types in C# and F#

                    P 1 Reply Last reply
                    0
                    • M Marc Clifton

                      Pete O'Hanlon wrote:

                      I've just had to ask the CodeProject team to restart the server as it's hosted on their servers.

                      Hmmm, it's still not responding. I guess the hamsters are on vacation. Marc

                      Reverse Engineering Legacy Applications
                      How To Think Like a Functional Programmer
                      My Blog
                      Computational Types in C# and F#

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #19

                      It's back Marc. Turns out that the server had been switched between Amazon instances and the DS settings hadn't been updated to point to the new instance. I'm putting monitoring in place to warn me if this happens again in the future.

                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      M 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        It's back Marc. Turns out that the server had been switched between Amazon instances and the DS settings hadn't been updated to point to the new instance. I'm putting monitoring in place to warn me if this happens again in the future.

                        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #20

                        Pete O'Hanlon wrote:

                        It's back Marc.

                        Thanks! One of the reasons I wanted to poke around the site was because I have another hair-brained idea that applications should consist of small, either re-usable or custom code blocks that are wired together with a tool like Visio. Ultimately I envision something that is a mix between what that website, code bubbles, does, and something like lego-building blocks where you can visually wire up the data flow and events. The actual granularity would be completely flexible, and I imagine larger scale building blocks for handling things like ORM's, etc. I'm still looking for a decent visualization tool, similar to Visio. Sacha's rework of the WPF layout tool looks like a very good starting point. So I wanted to see what people had contributed for snippets to 1) see if you were already doing some of this with CodeStash and 2) to see what people were contributing. Argh. I need time!!! Marc

                        Reverse Engineering Legacy Applications
                        How To Think Like a Functional Programmer
                        My Blog
                        Computational Types in C# and F#

                        P 1 Reply Last reply
                        0
                        • M Marc Clifton

                          Pete O'Hanlon wrote:

                          It's back Marc.

                          Thanks! One of the reasons I wanted to poke around the site was because I have another hair-brained idea that applications should consist of small, either re-usable or custom code blocks that are wired together with a tool like Visio. Ultimately I envision something that is a mix between what that website, code bubbles, does, and something like lego-building blocks where you can visually wire up the data flow and events. The actual granularity would be completely flexible, and I imagine larger scale building blocks for handling things like ORM's, etc. I'm still looking for a decent visualization tool, similar to Visio. Sacha's rework of the WPF layout tool looks like a very good starting point. So I wanted to see what people had contributed for snippets to 1) see if you were already doing some of this with CodeStash and 2) to see what people were contributing. Argh. I need time!!! Marc

                          Reverse Engineering Legacy Applications
                          How To Think Like a Functional Programmer
                          My Blog
                          Computational Types in C# and F#

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #21

                          If you need a hand, just let me know as this sounds fascinating. I'm currently planning quite a reimplementation of CodeStash based on feedback we've had, and the lessons we learnt building V1, so if there's anything that you need out of it, please let me know.

                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                          M 1 Reply Last reply
                          0
                          • P Pete OHanlon

                            If you need a hand, just let me know as this sounds fascinating. I'm currently planning quite a reimplementation of CodeStash based on feedback we've had, and the lessons we learnt building V1, so if there's anything that you need out of it, please let me know.

                            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #22

                            Pete O'Hanlon wrote:

                            If you need a hand, just let me know as this sounds fascinating.

                            I would really enjoy your participation! Can you send a private email so I can get your email address? What I'd like to do is storyboard a simple demonstration of the concept - I believe in writing the minimum amount of code necessary to get a conceptual prototype working! I'll send you the storyboard and I would like to discuss it with you. Marc

                            Reverse Engineering Legacy Applications
                            How To Think Like a Functional Programmer
                            My Blog
                            Computational Types in C# and F#

                            P 1 Reply Last reply
                            0
                            • M Marc Clifton

                              Pete O'Hanlon wrote:

                              If you need a hand, just let me know as this sounds fascinating.

                              I would really enjoy your participation! Can you send a private email so I can get your email address? What I'd like to do is storyboard a simple demonstration of the concept - I believe in writing the minimum amount of code necessary to get a conceptual prototype working! I'll send you the storyboard and I would like to discuss it with you. Marc

                              Reverse Engineering Legacy Applications
                              How To Think Like a Functional Programmer
                              My Blog
                              Computational Types in C# and F#

                              P Offline
                              P Offline
                              Pete OHanlon
                              wrote on last edited by
                              #23

                              Hopefully my email got through.

                              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                              M 1 Reply Last reply
                              0
                              • P Pete OHanlon

                                Hopefully my email got through.

                                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                M Offline
                                M Offline
                                Marc Clifton
                                wrote on last edited by
                                #24

                                Pete O'Hanlon wrote:

                                Hopefully my email got through.

                                Yes it did! I've been running around town today, but I just started putting together the concept as, what else, a Power Point slide deck! Mwahahaha! I'll email it to you as soon as I get a rough draft done. :) Marc

                                Reverse Engineering Legacy Applications
                                How To Think Like a Functional Programmer
                                My Blog
                                Computational Types in C# and F#

                                P 1 Reply Last reply
                                0
                                • M Marc Clifton

                                  Pete O'Hanlon wrote:

                                  Hopefully my email got through.

                                  Yes it did! I've been running around town today, but I just started putting together the concept as, what else, a Power Point slide deck! Mwahahaha! I'll email it to you as soon as I get a rough draft done. :) Marc

                                  Reverse Engineering Legacy Applications
                                  How To Think Like a Functional Programmer
                                  My Blog
                                  Computational Types in C# and F#

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #25

                                  Excellent. I look forward to seeing it.

                                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                  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