True or False Flags: best practices
-
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 ]
-
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 ]
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 :-)
-
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 ]
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.
-
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 ]
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] }
-
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 ]