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. General Programming
  3. Design and Architecture
  4. True or False Flags: best practices

True or False Flags: best practices

Scheduled Pinned Locked Moved Design and Architecture
questiondiscussion
5 Posts 4 Posters 2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    A few colleagues of mine are in disagreement as to what is the best practice for coding boolean flags. Should you use the actual words "true" or "false" or use the integers "1" or "0" (or -1 in some languages) respectively. I favor a boolean flag to show values as "true" or "false" because it makes reading 800 lines of code easier. 0's and 1's can be confusing, in my scenario, because we are constantly setting variables back to a regular integer values of 1 or 0. Thanks! -- Steve

    [ Don't do today what can be done tomorrow ]

    E P L L 4 Replies Last reply
    0
    • L Lost User

      A few colleagues of mine are in disagreement as to what is the best practice for coding boolean flags. Should you use the actual words "true" or "false" or use the integers "1" or "0" (or -1 in some languages) respectively. I favor a boolean flag to show values as "true" or "false" because it makes reading 800 lines of code easier. 0's and 1's can be confusing, in my scenario, because we are constantly setting variables back to a regular integer values of 1 or 0. Thanks! -- Steve

      [ Don't do today what can be done tomorrow ]

      E Offline
      E Offline
      Emmanouil
      wrote on last edited by
      #2

      I believe you are right. In terms of readability if the language you are using actually supports the boolean type and the true,false keywords then it means is recommended to use them. In certain cases this will not be true. For example C did not have a book type until C99 came out and then C++ provided bool type and true/false keywords as a built in feature. If you do use C++ then it is recommended to use the true/false type since they are provided. Even in C , it was a good coding practice to #define the keywords TRUE and FALSE to 1 and 0 respectively to provide a more readable code. Just my humble opinion :-)

      1 Reply Last reply
      0
      • L Lost User

        A few colleagues of mine are in disagreement as to what is the best practice for coding boolean flags. Should you use the actual words "true" or "false" or use the integers "1" or "0" (or -1 in some languages) respectively. I favor a boolean flag to show values as "true" or "false" because it makes reading 800 lines of code easier. 0's and 1's can be confusing, in my scenario, because we are constantly setting variables back to a regular integer values of 1 or 0. Thanks! -- Steve

        [ Don't do today what can be done tomorrow ]

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

        Steve I have to agree with you here. True/false are better, primarily because they are unambiguous. Using 1/-1 or 0 causes confusion - is it a number or a boolean condition? What happens if I add them together?

        Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

        1 Reply Last reply
        0
        • L Lost User

          A few colleagues of mine are in disagreement as to what is the best practice for coding boolean flags. Should you use the actual words "true" or "false" or use the integers "1" or "0" (or -1 in some languages) respectively. I favor a boolean flag to show values as "true" or "false" because it makes reading 800 lines of code easier. 0's and 1's can be confusing, in my scenario, because we are constantly setting variables back to a regular integer values of 1 or 0. Thanks! -- Steve

          [ Don't do today what can be done tomorrow ]

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, Of course I agree with the true/false syntax preference over 1/0. I want to add one big caveat: in C integer values other than 0 and 1 will fail the if (x==TRUE) test but pass the if(x) test, yielding some very nasty bugs. So the recommendation might be: 1. to use !=FALSE rather than ==TRUE 2. to use IS_TRUE(x) and IS_FALSE(x) macros instead 3. to use FALSE and !FALSE rather than FALSE and TRUE (but not ==!FALSE) None of these could be enforced however. :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          1 Reply Last reply
          0
          • L Lost User

            A few colleagues of mine are in disagreement as to what is the best practice for coding boolean flags. Should you use the actual words "true" or "false" or use the integers "1" or "0" (or -1 in some languages) respectively. I favor a boolean flag to show values as "true" or "false" because it makes reading 800 lines of code easier. 0's and 1's can be confusing, in my scenario, because we are constantly setting variables back to a regular integer values of 1 or 0. Thanks! -- Steve

            [ Don't do today what can be done tomorrow ]

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thanks for the input...good to know that I am not the only one who thinks that way about Boolean logic. :)

            [ Don't do today what can be done tomorrow ]

            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