Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. If (null == something) or if (something == null)

If (null == something) or if (something == null)

Scheduled Pinned Locked Moved C#
question
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

    ___________________________________________ .\\axxx (That's an 'M')

    L D T P L 6 Replies Last reply
    0
    • L Lost User

      A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

      ___________________________________________ .\\axxx (That's an 'M')

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, the origin is it protects you against a typo where you drop one equal sign; then constant=variable yields an error, whereas variable=constant may not (it wouldn't in C/C++, it most often would in C# unless the types are bool). (*) However, I am with you, it does not look good, and any decent compiler would normally (there are exceptions conceivable) generate a WARNING message, either saying: "are you sure that is what you intend?" or "condition will always be true/false". However MS compilers seem not to do so. [ADDED] (*) which is another good reason never to write things such as if (someBool==true)..., just write if (someBool) .... [/ADDED] :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


      modified on Thursday, July 2, 2009 8:10 PM

      1 Reply Last reply
      0
      • L Lost User

        A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

        ___________________________________________ .\\axxx (That's an 'M')

        D Offline
        D Offline
        Dave Doknjas
        wrote on last edited by
        #3

        The "null == something" form (or in general, " == " form), is simply not useful in C# or Java. The purpose of this form was to avoid accidentally typing an assignment rather than a comparison in C/C++. C# and Java will not let you make this mistake since the condition must evaluate to a boolean and a simple assignment will not (unless you compare the assignment to something).

        David Anton http://www.tangiblesoftwaresolutions.com Convert VB to C#, C++, or Java Convert C# to VB, C++, or Java Convert C++ to C#, VB, or Java Convert Java to C#, C++, or VB

        1 Reply Last reply
        0
        • L Lost User

          A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

          ___________________________________________ .\\axxx (That's an 'M')

          T Offline
          T Offline
          Thomas Weller 0
          wrote on last edited by
          #4

          _Maxxx_ wrote:

          is there a good reason for using the former over the latter?

          There is, and AFAIK this reason comes from the good old C/C++ days. It was a very common error (the most common of all) to type if (reason = null) instead of if (reason == null). This small little typo led to very strange program behaviour and was very hard to debug, because it's very easy to overread it when inspecting the code. The problem was that the C/C++ - Compiler didn't even issue a warning when you typed something like if (reason = null), but it immediately complained about if (null = reason). So it's all about correctness and maintainability. I even saw companies with coding guidelines that dictated this style. Regards Thomas

          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
          Programmer - an organism that turns coffee into software.

          1 Reply Last reply
          0
          • L Lost User

            A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

            ___________________________________________ .\\axxx (That's an 'M')

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yeah, what they said. It only works when comparing an Rvalue to an Lvalue, which isn't necessarily all that frequently, and my argument against it is that if I can remember to write it that way, then I will probably not make that mistake anyway so it's fairly pointless. I worked for a company with the former specified in the company coding standard (for writing C), but by the time I left, even the "guru" (who had written the standard) had admitted that it wasn't that important and wasn't enforcing it.

            1 Reply Last reply
            0
            • L Lost User

              A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

              ___________________________________________ .\\axxx (That's an 'M')

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!

              ___________________________________________ .\\axxx (That's an 'M')

              M M P 3 Replies Last reply
              0
              • L Lost User

                So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!

                ___________________________________________ .\\axxx (That's an 'M')

                M Offline
                M Offline
                Moim Hossain
                wrote on last edited by
                #7

                _Maxxx_ wrote:

                Sack 'em,. I say, Sack 'em!

                I am also in your side that this is not that readable comapring the other version of it (ie. something == null - which is more simpler and expected), but I dont agree with you that (null==something) is that much "unreadable" that you need to sack somebody. If you have hardship reading this style, according to your way of thought, you are also a good candidate to get sacked. :laugh:

                Moim Hossain R&D Project Manager BlueCielo ECM Solutions BV

                1 Reply Last reply
                0
                • L Lost User

                  A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?

                  ___________________________________________ .\\axxx (That's an 'M')

                  B Offline
                  B Offline
                  btough
                  wrote on last edited by
                  #8

                  If (null == something) and if (something == null) are nothing but the same. But I/many people always prefer - If (null == something), because if we use - if(something == null), sometimes it may happens that instead of == we use = and that change the value of 'something'. And if 'something' is used throuout the programe, you can imagine how wrong result it will create. So using If (null == something) is always be safe. Hope this will be helpful to you. Thanks, -Yogesh Patil.

                  1 Reply Last reply
                  0
                  • L Lost User

                    So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!

                    ___________________________________________ .\\axxx (That's an 'M')

                    M Offline
                    M Offline
                    moon_stick
                    wrote on last edited by
                    #9

                    _Maxxx_ wrote:

                    Sack 'em,. I say, Sack 'em!

                    On a serious note, people who write code this way are generally people who've used something like C and have been caught out by assigning a value rather than doing a comparison. The fact that they've made a conscious effort to change their coding style to make sure that their code is less error prone is probably an indicator of a good developer, rather than anything else.

                    It definitely isn't definatley

                    1 Reply Last reply
                    0
                    • L Lost User

                      So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!

                      ___________________________________________ .\\axxx (That's an 'M')

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Right, but even "modern" C compilers will issue a warning if the mistake is made:

                      Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
                      aa.c:
                      Warning W8060 aa.c 25: Possibly incorrect assignment in function main

                      And HP C:

                      if ( result = 5 )
                      

                      ....^
                      %CC-I-CONTROLASSIGN, In this statement, the assignment expression "result=5" is used as the controlling expression of an if, while or for statement.
                      at line number 13 in file MY$ROOT:[000000]AA.C;1

                      The D language defines the mistake as an error.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups