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 this 'null' check doing here...

What this 'null' check doing here...

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpdotnetwcf
59 Posts 25 Posters 4 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.
  • M Mohammed Hameed

    List<Employee> employees = new List<Employee>();

    if (employees != null){
    employees = GetEmployees();
    }

    Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

    N Offline
    N Offline
    Nilesh Shahane
    wrote on last edited by
    #31

    He is just double checking. :)

    M 1 Reply Last reply
    0
    • E englebart

      More like the garbage collector...

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #32

      That's a real conundrum for the GC - "OK, if this object is still around, at this point, I can't GC it until then. But... but what if I ... secretlly GC it before the if?"

      ORDER BY what user wants

      1 Reply Last reply
      0
      • M Mohammed Hameed

        List<Employee> employees = new List<Employee>();

        if (employees != null){
        employees = GetEmployees();
        }

        Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

        M Offline
        M Offline
        MainFrameMan_ALIVE_AND_WELL
        wrote on last edited by
        #33

        Looks like a newbie whose prof in 101 programming hammered down checking objects before using them. This would be sensible if it were a thousand lines down in the code, but, if it is null where is the property/method to new it up?

        M 1 Reply Last reply
        0
        • P Pete OHanlon

          Never mind - I've been up for the last 16 hours. I was reading != as == here for some reason. :doh:

          I was brought up to respect my elders. I don't respect many people nowadays.
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          K Offline
          K Offline
          KP Lee
          wrote on last edited by
          #34

          Pete O'Hanlon wrote:

          I was reading != as == here

          Thanks for the explanation, for a normally sensible person, I was wondering what you were smoking or what I missed with your prior comment too.

          1 Reply Last reply
          0
          • G GrumpyPants

            Check your version control. It's possible declaration and/or the assignment at declaration was added later. If an earlier version of the method had the List passed as a parameter, then the test for non-null would make sense.

            M Offline
            M Offline
            Mohammed Hameed
            wrote on last edited by
            #35

            Quote:

            It's possible declaration and/or the assignment at declaration was added later.

            Ok but in the case if it is added later also, the guy should have removed the null check as it is not applicable now. Anyhow that wasn't the case, I checked the source control :)

            Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

            1 Reply Last reply
            0
            • D David C Thompson

              Probably code that has been edited and then not refactored. No real wtf here, ey?

              M Offline
              M Offline
              Mohammed Hameed
              wrote on last edited by
              #36

              But that's not the case I have verified the previous versions from source control...

              Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

              D 1 Reply Last reply
              0
              • R RafagaX

                I bet the person who wrote this comes from a C/C++ background, there, you have to check for null every time or you may blow out something...

                CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                M Offline
                M Offline
                Mohammed Hameed
                wrote on last edited by
                #37

                Correct. *** You're superb,,, What a guess!!! *** :thumbsup:

                Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                1 Reply Last reply
                0
                • M Michael Losinski

                  The only thing that I can think of is technically new doesn't guarantee that the variable initialization will take place. In programming languages like c++ it could return null. This could happen if the memory was so fragmented that the allocation failed. However, instead of returning null in C# I believe it throws a out of memory exception. I have only seen this done in system critical embedded systems.

                  M Offline
                  M Offline
                  Mohammed Hameed
                  wrote on last edited by
                  #38

                  Thanks Michael. That was a excellent explanation.

                  Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                  1 Reply Last reply
                  0
                  • N Nilesh Shahane

                    He is just double checking. :)

                    M Offline
                    M Offline
                    Mohammed Hameed
                    wrote on last edited by
                    #39

                    :laugh: The same thing I told him when I saw this first time. :)

                    Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                    1 Reply Last reply
                    0
                    • M MainFrameMan_ALIVE_AND_WELL

                      Looks like a newbie whose prof in 101 programming hammered down checking objects before using them. This would be sensible if it were a thousand lines down in the code, but, if it is null where is the property/method to new it up?

                      M Offline
                      M Offline
                      Mohammed Hameed
                      wrote on last edited by
                      #40

                      Well, he is 4 yrs exp.....

                      Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                      K 1 Reply Last reply
                      0
                      • E englebart

                        More like the garbage collector...

                        M Offline
                        M Offline
                        Mohammed Hameed
                        wrote on last edited by
                        #41

                        :)

                        Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                        1 Reply Last reply
                        0
                        • B bencr

                          It isn't really filling the list, it's replacing the reference with the result of that method call. I don't think that's what he meant though. A bit pedantic, I'm sorry.

                          K Offline
                          K Offline
                          KP Lee
                          wrote on last edited by
                          #42

                          I think the point was that

                            List employees = GetEmployees();
                          

                          could just as well have been used to create a null or non-null employees object because the if statement would never be false for a statement that could produce a null result. In fact you might want to execute

                            if (employees == null) throw...
                          

                          after executing the above line because now you are in a situation where the if statement could be true or false even if the current coding of the routine would never return null. (speaking of being pedantic...)

                          1 Reply Last reply
                          0
                          • Z ZurdoDev

                            Do you two realize how much money you just cost CP with this waste of disk space? You better click on some ads to pay for your mistakes.

                            There are only 10 types of people in the world, those who understand binary and those who don't.

                            M Offline
                            M Offline
                            Mohammed Hameed
                            wrote on last edited by
                            #43

                            What was that message??, now its not there

                            Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                            Z 1 Reply Last reply
                            0
                            • M Mark Starr

                              Guess I don't know c# well enough, but I didn't this you could use any comparison operator against NULL. it's probably bad practice to even if the language allows it.

                              K Offline
                              K Offline
                              KP Lee
                              wrote on last edited by
                              #44

                              Mark Starr wrote:

                              I didn't this you could use any comparison operator against NULL

                              You're mixing up SQL with C# as well as "this" vs "think". SQL isn't case sensitive and comparison operators won't work with null, NULL, or NuLl where all versions of the null keyword are valid. In C#, NULL is nonsense unless you've declared an object name NULL and the language is designed to make comparisons (==, !=) with null

                              M 1 Reply Last reply
                              0
                              • M Mohammed Hameed

                                But that's not the case I have verified the previous versions from source control...

                                Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                                D Offline
                                D Offline
                                David C Thompson
                                wrote on last edited by
                                #45

                                lol. Then its probably just a brain fart, or late night coding session :)

                                1 Reply Last reply
                                0
                                • M Michael Losinski

                                  The only thing that I can think of is technically new doesn't guarantee that the variable initialization will take place. In programming languages like c++ it could return null. This could happen if the memory was so fragmented that the allocation failed. However, instead of returning null in C# I believe it throws a out of memory exception. I have only seen this done in system critical embedded systems.

                                  K Offline
                                  K Offline
                                  KP Lee
                                  wrote on last edited by
                                  #46

                                  Michael Losinski wrote:

                                  The only thing that I can think of is technically new doesn't guarantee that the variable initialization will take place.

                                  Who cares? If the "if" check returns true, the result of calling the routine could make it null. If it's mission critical, initialize it by calling the function and then throw it when the function sets it to null. (Which it doesn't seem to check for, when that is a very possible result of calling the function.) If you are so constrained by memory the new would fail, it'd fail in the function too, thereby throwing an error the code doesn't check for(catch).

                                  M 1 Reply Last reply
                                  0
                                  • M Mohammed Hameed

                                    Well, he is 4 yrs exp.....

                                    Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                                    K Offline
                                    K Offline
                                    KP Lee
                                    wrote on last edited by
                                    #47

                                    Mohammed Hameed wrote:

                                    Well, he is 4 yrs exp....

                                    So, he IS inexperienced! :-D (I started coding in '74. Late start, '05 on C#.)

                                    M 1 Reply Last reply
                                    0
                                    • K KP Lee

                                      Mohammed Hameed wrote:

                                      Well, he is 4 yrs exp....

                                      So, he IS inexperienced! :-D (I started coding in '74. Late start, '05 on C#.)

                                      M Offline
                                      M Offline
                                      Mohammed Hameed
                                      wrote on last edited by
                                      #48

                                      Ohh, is that?

                                      Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                                      1 Reply Last reply
                                      0
                                      • M Mohammed Hameed

                                        What was that message??, now its not there

                                        Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                                        Z Offline
                                        Z Offline
                                        ZurdoDev
                                        wrote on last edited by
                                        #49

                                        Weird, I wonder why they were removed. They just had a ton of smiley face icons.

                                        There are only 10 types of people in the world, those who understand binary and those who don't.

                                        M 1 Reply Last reply
                                        0
                                        • Z ZurdoDev

                                          Weird, I wonder why they were removed. They just had a ton of smiley face icons.

                                          There are only 10 types of people in the world, those who understand binary and those who don't.

                                          M Offline
                                          M Offline
                                          Mohammed Hameed
                                          wrote on last edited by
                                          #50

                                          May be because of "Smiley Overflows" :laugh:

                                          Previous -> Read "CLR via C#" by Jeffrey Ritcher. Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder. Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.

                                          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