Monday, June 08, 2009

It's "true" and "false", not "TRUE" and "FALSE"!!!

One thing that bugs me is seeing PHP code like this:

$somevar = FALSE;


if ($somevar == TRUE)

Why? Because in very common convention, UPPERCASE is reserved for constants. This follows on from the languages on which PHP is syntactically based, such as C and C++, and most code that I have seen (and written) continues this convention.

The thing is though, true and false are *not* constants: they are keywords.

As a different way of explaining that, consider how one would write the define statement for TRUE and FALSE, without being recursive. The best I can come up with is:

define('TRUE', (bool)1);

define('FALSE', (bool)0);

It's like saying that in order to be able to use 1 in your code you need a define like this:

define('1', 4-3);

Which is clearly insane.

Happily, we don't have to jump through such hoops to get at the boolean values, and it is not via some pre-defined constant; it is via the keywords "true" and "false", which given that they are keywords and not constants, should be written in lower case.

Or as a friend one put it "But... they're blue!" - Referring to the fact that his editor hilighted them as blue along with all other keywords.

No comments: