Wednesday, December 7. 2005($var == TRUE) or (TRUE == $var)?Comments
Display comments as
(Linear | Threaded)
I understand the possible arguments for if(FALSE == $myvar), but I don't like it.
The subject you are interested in should come first, as in proper language (spoken, even). Asking if false is the same as some variable is masking the fact that the variable is what we're interested in. It reminds me of the movie "Way of the Gun" when a guy is at a sperm donor bank to donate for cash and in the interview, the interviewer is filling out his vital stats form and asks "Heterosexual?". Of course, it makes more sense for the interviewer to ask if he is homosexual, because that's *really* what he is trying to find out, however he's dancing around it.
Did you follow the zend exam ? because this example is also in the study book :)
I agree with hermit. Code should be close to the human language (one of the reasons why I haven't started with Ruby yet, the syntax is not my cup of tea).
Changing your syntax to avoid semantical bugs? Some decent unit tests (and we are probably all guilty of writing too few ;-) should be the answer, or am I mistaken?
I've been using this for quite some time now and it saved me lot of troubles. So I absolutely agree that it's useful. It may seem a little bit awkward at first, but since it's not really obscure, I'd say it's fine.
I have been doing this for a few years. It is absolutely the best way to do things. A real time saver.
In most of my code, I'm comparing stuff to other stuff. There's only a portion that's compared with constants. I find the decreased chance of assignment errors not worth the decreased readability of the code.
I suspect that in more complex comparisons, the chances of making mistakes in 'backwards' code is higher than the chance of a regular assignment error. Also, aren't there IDE's out there that warn for this type of mistake?
Balony! Code is written to be read by coders, and to be compiled/interpreted only by computers. I would appreciate if there was some sort of precompiler that would just swap them around before running the code. But this is awful.
To prevent this type of error, and lots of others, start using assert(). That's easier to come up with then a unit test case, and if done properly does the very same thing, only at run time. Chances are very very small that you'd make the same mistake in the assert again.
Lots of people recommend doing the checking variables that way so that you don't accidently do variable assignment during if checking. It's a mission to change coding style ;)
Maybe Pascal's usage of ":=" as assignment operator isn't that out of place after all. I agree that code should be as readable as possible, hence the variable should come first...
Maybe a compiler notice for "possible non-intended assignement in boolean evaluation" if an assignment takes place within an if() clause...? Althought that would also highlight things like: if (!$result = $database->query($sql)) But since it would be just a notice...
Yeah, I think Pascal got it right, assignment is simply not the same as equivalence, and the := operator reflects that. As to the reverse comparisons, it's in the C FAQ somewhere.
On occasion I have toyed with that syntax, but yes, it runs contrary to literate programming. In the end I decided to try to write readable code, and not funky / overly clever code. |
Pay the bills...Calendar
QuicksearchArchivesMy PERSONAL BlogNote:
The contents of this blog are my own, PERSONAL opinion and do not represent the thoughts or opinions of the people I work with or for. If you have a beef with something I said, take it up with ME. Thanks (-: |
|||||||||||||||||||||||||||||||||||||||||||||||||
I've been doing a lot of looking around at PHP stuff recently, as well as doing a bunch of PHP coding -- something I'd dropped doing a while ago. First, I found some interesting stats about PHP's use, citing PHP's use at 22,172,983 Domains, and 1,277,
Tracked: Jan 28, 03:25