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 5 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.
  • B Brisingr Aerowing

    :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh: :doh:

    J Offline
    J Offline
    Johnny YYZ
    wrote on last edited by
    #23

    You owe me for the bandwidth cost of 2396 unnecessary :doh:s! Only the icon has 831 bytes, which amounts to a total of 1,991,076 wasted bytes. Gotta count now the extra html chars and send you the invoice! :doh: :sigh: :laugh:

    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.

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

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

      M 1 Reply Last reply
      0
      • M Mohammed Hameed

        Quote:

        Is this inside a single method?

        Yes, it is.

        Quote:

        I'd be more concerned here with the fact you'd never fill the employees list.

        How??

        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.

        B Offline
        B Offline
        bencr
        wrote on last edited by
        #25

        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 1 Reply Last reply
        0
        • P peterchen

          Keeping the runtime on its toes.

          ORDER BY what user wants

          E Offline
          E Offline
          englebart
          wrote on last edited by
          #26

          More like the garbage collector...

          P M 2 Replies 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
            Mark Starr
            wrote on last edited by
            #27

            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 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.

              G Offline
              G Offline
              GrumpyPants
              wrote on last edited by
              #28

              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 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.

                R Offline
                R Offline
                RafagaX
                wrote on last edited by
                #29

                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 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
                  Michael Losinski
                  wrote on last edited by
                  #30

                  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 K 2 Replies 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.

                    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
                                          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