1. OCAU Merchandise now available! Check out our 20th Anniversary Mugs, Classic Logo Shirts and much more! Discussion here.
    Dismiss Notice

Best practice

Discussion in 'Programming & Software Development' started by rockuman_ex, May 13, 2020.

  1. rockuman_ex

    rockuman_ex Member

    Joined:
    Apr 3, 2002
    Messages:
    3,840
    Location:
    Brisbane
    Which is the best practice

    if ($blah == 0)

    or

    if (0 == $blah)
     
  2. Caffeine

    Caffeine Member

    Joined:
    Jul 1, 2003
    Messages:
    1,582
    Location:
    Sydney
    I've seen people claim that if(0==$blah) is faster but generally the idea of that syntax is that the compiler will give an error if you accidentally type if (0=$blah)

    Any good compiler will warn you about that these days so it doesn't matter
     
  3. OP
    OP
    rockuman_ex

    rockuman_ex Member

    Joined:
    Apr 3, 2002
    Messages:
    3,840
    Location:
    Brisbane
    yeah, thank you :)
     
  4. Primüs

    Primüs Member

    Joined:
    Apr 1, 2003
    Messages:
    3,405
    Location:
    CFS
    Unless you are going for ultra optimisation (and lets be honest, im talking millions of hits kind of optimisation) then you should go for readability, where

    Code:
    if($blah == 0) {
    
    Is much more standard, and reads much better because you are comparing variable to value not value to variable, because when you are assigning a value to a variable you are also saying $blah = 0; (At least, in most languages)
     
  5. ashenIC

    ashenIC Member

    Joined:
    Sep 21, 2019
    Messages:
    5
    They are both readable. Best practice, doesn't matter. Optimisation see what the compiler (for compiled languages) produces. What if you assigned zero to a constant variable if ($const_var_zero == $blah) is the same as if ($blah == $const_var_zero).
     
  6. theSeekerr

    theSeekerr Member

    Joined:
    Jan 19, 2010
    Messages:
    3,305
    Location:
    Broadview SA
    Very strict practices like the second form have their place in highly defensive coding styles for safety-critical applications, like MISRA C.

    But the first form reads "grammatically" to most people and should probably be preferred most of the time.
     

Share This Page

Advertisement: