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. Other Discussions
  3. The Weird and The Wonderful
  4. What is null equal to?

What is null equal to?

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
23 Posts 16 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.
  • J jeron1

    sloosecannon wrote:

    What is null..... Equal to?

    Don't know, I'm having difficulty getting past the brace placement.

    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

    S Offline
    S Offline
    sloosecannon
    wrote on last edited by
    #13

    Eh, not my choice. Code style rules...

    1 Reply Last reply
    0
    • V Vark111

      Well, clearly they didn't apply the proper Yoda-ordered syntax in the if statement. Should have read thus:

      if (null == value || (null).equals(value)) {

      S Offline
      S Offline
      sloosecannon
      wrote on last edited by
      #14

      Which can be simplified to

      if (null==value)
      {
      throw new NullPointerException();//It was null, we don't like their kind here
      }
      else
      {
      //Do something
      }

      L 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        See the text at the top of the page: "a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance." Nearly everything here is found in real code and will hopefully make you go :doh: :WTF: :OMG: And laugh that anyone could think that the right thing to do... Generally, explanations and code fragment after the original are expected to make even less sense: just to prove it can be done! :laugh: For example, another way to do the original method would be

        public object IsNull(object o)
        {
        try
        {
        return o.Equals(null) ? o : o;
        }
        catch
        {
        return null;
        }
        }

        But you'd have to be a complete moron to write that! Oops. I just did... :-O

        You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

        R Offline
        R Offline
        Rob Grainger
        wrote on last edited by
        #15

        OriginalGriff wrote:

        But you'd have to be a complete moron to write that!

        Q.E.D. (Sorry too easy)

        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

        1 Reply Last reply
        0
        • J jeron1

          sloosecannon wrote:

          What is null..... Equal to?

          Don't know, I'm having difficulty getting past the brace placement.

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

          R Offline
          R Offline
          Rob Grainger
          wrote on last edited by
          #16

          Nothing wrong with that. It's very common. K&R Style[^]

          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

          1 Reply Last reply
          0
          • S sloosecannon

            Which can be simplified to

            if (null==value)
            {
            throw new NullPointerException();//It was null, we don't like their kind here
            }
            else
            {
            //Do something
            }

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

            If it was VB, yes, but not in C#. If it is null, the second part will not be evaluated. It's effectively a dead condition. So, it won't blow up - thanks to this oversight. I vaguely remember a manager who claimed that we always should use ".equals" for comparisons, and that it was a best practice. Using the operator is not only more readable, it also does not depend on the object having a value.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            S Richard DeemingR 2 Replies Last reply
            0
            • L Lost User

              If it was VB, yes, but not in C#. If it is null, the second part will not be evaluated. It's effectively a dead condition. So, it won't blow up - thanks to this oversight. I vaguely remember a manager who claimed that we always should use ".equals" for comparisons, and that it was a best practice. Using the operator is not only more readable, it also does not depend on the object having a value.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              S Offline
              S Offline
              sloosecannon
              wrote on last edited by
              #18

              Woops, logic derp. Yep, you're right. The code is Java for context, but it does lazy logic evaluation too...

              1 Reply Last reply
              0
              • L Lost User

                If it was VB, yes, but not in C#. If it is null, the second part will not be evaluated. It's effectively a dead condition. So, it won't blow up - thanks to this oversight. I vaguely remember a manager who claimed that we always should use ".equals" for comparisons, and that it was a best practice. Using the operator is not only more readable, it also does not depend on the object having a value.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #19

                Taking the code from Vark111's post, and assuming C#:

                if (null == value || (null).Equals(value)) {

                If value isn't null, the second part will be evaluated. Since the second part tries to call the Equals method on a null reference, it would throw a NullReferenceException. :doh: Thankfully, the C# compiler is smart enough to prevent you from compiling this code - you'll get an "Operator '.' cannot be applied to operand of type '<null>'" compiler error.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • S sloosecannon

                  I come before you with a question, a yearning in my heart. What is null..... Equal to?

                  ...
                  if (value == null || value.equals(null)) {
                  return "null";
                  }
                  ...

                  Evidently, the concept of "null" and "null pointers" and "null pointer calls" is lost on the guys who wrote this particular line of code. It doesn't make any errors, probably because someone added the first condition after a few NPEs happened... EDIT: Clarification - the title is rhetorical.

                  C Offline
                  C Offline
                  ColborneGreg
                  wrote on last edited by
                  #20

                  Empty memory is a variable that is declared without a value Null memory is a variable that does not have a memory location Same goes with null from files.

                  1 Reply Last reply
                  0
                  • S sloosecannon

                    I come before you with a question, a yearning in my heart. What is null..... Equal to?

                    ...
                    if (value == null || value.equals(null)) {
                    return "null";
                    }
                    ...

                    Evidently, the concept of "null" and "null pointers" and "null pointer calls" is lost on the guys who wrote this particular line of code. It doesn't make any errors, probably because someone added the first condition after a few NPEs happened... EDIT: Clarification - the title is rhetorical.

                    S Offline
                    S Offline
                    sankarsan parida
                    wrote on last edited by
                    #21

                    “==” compares if the object references are same while “.Equals()” compares if the contents are same. So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.

                    object o = ".NET Interview questions";
                    object o1 = o;
                    Console.WriteLine(o == o1);
                    Console.WriteLine(o.Equals(o1));
                    Console.ReadLine();

                    True True Now consider the below code where we have same content but they point towards different instances. So if you run the below code both “==” will return false and “.Equals()” will return true.

                    object o = ".NET Interview questions";
                    object o1 = new string(".NET Interview questions".ToCharArray());
                    Console.WriteLine(o == o1);
                    Console.WriteLine(o.Equals(o1));
                    Console.ReadLine();

                    False True

                    Sankarsan Parida

                    1 Reply Last reply
                    0
                    • S sloosecannon

                      I come before you with a question, a yearning in my heart. What is null..... Equal to?

                      ...
                      if (value == null || value.equals(null)) {
                      return "null";
                      }
                      ...

                      Evidently, the concept of "null" and "null pointers" and "null pointer calls" is lost on the guys who wrote this particular line of code. It doesn't make any errors, probably because someone added the first condition after a few NPEs happened... EDIT: Clarification - the title is rhetorical.

                      M Offline
                      M Offline
                      Munchies_Matt
                      wrote on last edited by
                      #22

                      sloosecannon wrote:

                      What is null equal to?

                      The place I worked in Germany?

                      Sign a petition calling for the boycott of Israel until it returns to its legal 1967 borders.

                      1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        See the text at the top of the page: "a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance." Nearly everything here is found in real code and will hopefully make you go :doh: :WTF: :OMG: And laugh that anyone could think that the right thing to do... Generally, explanations and code fragment after the original are expected to make even less sense: just to prove it can be done! :laugh: For example, another way to do the original method would be

                        public object IsNull(object o)
                        {
                        try
                        {
                        return o.Equals(null) ? o : o;
                        }
                        catch
                        {
                        return null;
                        }
                        }

                        But you'd have to be a complete moron to write that! Oops. I just did... :-O

                        You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

                        R Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #23

                        Q.E.D. (Sorry, it was just sitting there waiting for it).

                        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                        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