12

Possible Duplicate:
Why do some experienced programmers write comparisons with the value before the variable?

I am just curious about this: in most frameworks/opensource projects I have studied, I often seen code like this...

<?php

if (null === self::$_instance) {
    self::$_instance = new self();
}

In particular this line...

if (null === self::$_instance) {

Why use null in the first argument of the if statement instead of the other way around?...

if (self::$_instance === null) {

I realize there is probably no performance increase or anything like that. Is this just a preference or is it some kind of coding standard I have overlooked?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 2
    Personally, I am inclined to say that they all should be `if( !self::$_instance )` – cwallenpoole Aug 31 '11 at 17:48
  • If this isn't a dup I'll eat my keyboard – Lightness Races in Orbit Aug 31 '11 at 17:49
  • 5
    That notation style is know as "yoda expressions" or "yoda conditions". – mario Aug 31 '11 at 17:50
  • 1
    Glad you asked! I have always been curious, and I don't really like the null-first style. If there's a good reason for it though, maybe I'll adopt it. – pseudoramble Aug 31 '11 at 17:50
  • 2
    @cwallenpoole: The OP is testing for exact equality to null, but your example would also match false, zero, and empty string. – Bill Karwin Aug 31 '11 at 17:50
  • 1
    Putting expressions on the left and variables on the right side of a condition can help tremendeous in some cases. E.g. if you want to compare s/t with `==` but miss or delete an equal sign by accident, then an `if ($x = 1)` will just evaluate w/o notice, whereas `if (1 = $x)` would throw an error message. – Jürgen Thelen Aug 31 '11 at 17:55
  • @Bill This looks like a Singleton pattern. If it is, then $_instance *better* not be `0`, `FALSE`, or `''`. If it isn't, then the authors of the framework are a little goofy. – cwallenpoole Aug 31 '11 at 18:19
  • @cwallenpoole: *Most* authors of frameworks are a little goofy. ;-) – Bill Karwin Aug 31 '11 at 18:21
  • 1
    @Bill as someone authoring a framework, I represent that remark! – cwallenpoole Aug 31 '11 at 18:22
  • 2
    Possible duplicate of [Why do some experienced programmers write comparisons with the value before the variable?](https://stackoverflow.com/questions/3309089/why-do-some-experienced-programmers-write-comparisons-with-the-value-before-the) – Cœur Jul 14 '18 at 07:03

5 Answers5

14

It prevents you from accidentally assigning the value to a variable, especially when only using loose type comparison (==):

if (self::$_instance = NULL) { … } // WHOOPS!, self::$_instance is now NULL

This style of conditions is often called yoda conditions. Performance wise there is no difference, both statements are equivalent.

Community
  • 1
  • 1
knittl
  • 246,190
  • 53
  • 318
  • 364
6

This is mainly to prevent accidental assignment:

if (self::$_instance = null) ... //oops!
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
4

There's no significant performance difference. The usual benefit of writing expressions in this way is defensive programming. We want to avoid accidentally using an assignment instead of equality comparison:

if (self::$_instance = null) { ...

Woops!

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
4

It's to help you get your code right.

If you do this, your code will work, but the effect will be a long way from what you want:

if (self::$instance = null) {

The conditional will always fail (because the = operator returns the value set, and it is falsy) but self::$instance will now be set to null. This isn't what you want.

If you do this:

if (null = self::$instance) {

your code will fail to work, because you can't use null (or any literal such as a string or an integer) on the left-hand-side of an assignment. Only variables can be the left-hand-side of the = operator.

So if you mistype the == as =, you get a parse error and your code completely doesn't work. This is preferable to a mystifying and hard-to-track-down bug.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

It's not particular to null - I've seen many coders prefer to write their expressions this way round:

if(8 == 4 * 2) {

It's just a preference which some people think is clearer.

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99