eg:
var value = true
if (value = true) {
}
or :
var value = 1
if (value = 1) {
}
Does it make any difference regarding to the way or the speed at which the script is executed? What do professionals/yourself use?
Thanks for your help, Colin
ps The language which I'm refering to in particular is javascript.
False and True vs 1 and 0 - which should I use?!?
Donald Knuth, a god of programming, said good code should be self-documenting. Of course there are always comments but he meant more than that. It doesn't matter what language you are using. If you happen to walk away from your code for a few months or years, then when you come back true and false will mean a lot more to you than 1 and 0. Therefore I strongly suggest you go with true and false.False and True vs 1 and 0 - which should I use?!?
if a language has true/false boolean values, use those to mean true or false!
and beware of the difference between = and ==, = assigns, == is a equality test, so the if is
if (value == true) {
}
and of course it is also self-explaining and common practice
if ( value ) {
}
You should be using:
var value = true
if (value == true) {
}
even though this also works
var value = true
if (value) // this automatically checks if the value is true or false
{
}
It really makes no difference.
I prefer setting value=true
Then, you don't need if (value=true). That's redundant. You only need to say if (value).
Of course professionals use boolean values.
For Javascript :
var x = new Boolean(expression);
Possible values : true or false (0, Null, N/A, ...)
No comments:
Post a Comment