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.
  • 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
              • K KP Lee

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

                Yes you could the code down to one line of code. You could catch the exception somewhere else, so I suppose the question could then become what do you do in a mission critical system when outofmemory exception or an allocation fails, therefore allowing you to gracefully shutdown. First thing that comes to mind is if the current thread is not mission critical you can kill that can free up some memory. Second, you could have a fail-safe that would allow you to kill all non-mission critical threads thus freeing memory. With the right permissions you could also kill the other applications. I've never implemented one but you could also have a "memory para-shoot" that at the start of the program allocates a chunk of memory and frees it when needed. I suppose allot of it depend on the system and requirements.

                1 Reply Last reply
                0
                • K KP Lee

                  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 Offline
                  M Offline
                  Mark Starr
                  wrote on last edited by
                  #52

                  Yup: I don't know C# well enough. Agreed. And, I didn't capitalize the 'i' at the beginning of the next sentence.

                  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.

                    D Offline
                    D Offline
                    DarkChuky CR
                    wrote on last edited by
                    #53

                    it could make sense if it was a code update... let say it was like:

                    List<Employee> employees = GetEmployessFromSucursal(sucursalID);

                    if (employees != null){
                    employees = GetEmployees();//Get employes from other source
                    }

                    then for some reason the code was updated or copy&paste... there is also a proba that the IF used to have some other parameter and an ELSE... so again code that was not finally updated to the best final form.

                    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.

                      C Offline
                      C Offline
                      Chad3F
                      wrote on last edited by
                      #54

                      Now a smart compiler should probably want about this. Something like when comparing signed and unsigned int's in C and it warns that the statement is always true (or false) due to conversion rules. Seems like maybe (I know.. "maybe?!?!") a programmer that didn't know what they're doing. That you have to first "create" the list, so you can "fill it in" with the GetEmployees() values or something. Along the same lines as novice programmers that create and assign a local number (or string) variable with a constant value because the function was prototyped with that data type, rather than pass the same constant number inline to the call. Could be worse.. I've seen this in [employed to write] software:

                      #define ZERO 0
                      #define ONE 1
                      #define TWO 2

                      :doh:

                      M C 2 Replies Last reply
                      0
                      • C Chad3F

                        Now a smart compiler should probably want about this. Something like when comparing signed and unsigned int's in C and it warns that the statement is always true (or false) due to conversion rules. Seems like maybe (I know.. "maybe?!?!") a programmer that didn't know what they're doing. That you have to first "create" the list, so you can "fill it in" with the GetEmployees() values or something. Along the same lines as novice programmers that create and assign a local number (or string) variable with a constant value because the function was prototyped with that data type, rather than pass the same constant number inline to the call. Could be worse.. I've seen this in [employed to write] software:

                        #define ZERO 0
                        #define ONE 1
                        #define TWO 2

                        :doh:

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

                        Thanks.

                        My Reading-o-Meter

                        Previous -> Read "CLR via C#" by Jeffrey Richter. 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.

                        My blog - My recent article

                        1 Reply Last reply
                        0
                        • D DarkChuky CR

                          it could make sense if it was a code update... let say it was like:

                          List<Employee> employees = GetEmployessFromSucursal(sucursalID);

                          if (employees != null){
                          employees = GetEmployees();//Get employes from other source
                          }

                          then for some reason the code was updated or copy&paste... there is also a proba that the IF used to have some other parameter and an ELSE... so again code that was not finally updated to the best final form.

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

                          Thanks.

                          My Reading-o-Meter

                          Previous -> Read "CLR via C#" by Jeffrey Richter. 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.

                          My blog - My recent article

                          1 Reply Last reply
                          0
                          • C Chad3F

                            Now a smart compiler should probably want about this. Something like when comparing signed and unsigned int's in C and it warns that the statement is always true (or false) due to conversion rules. Seems like maybe (I know.. "maybe?!?!") a programmer that didn't know what they're doing. That you have to first "create" the list, so you can "fill it in" with the GetEmployees() values or something. Along the same lines as novice programmers that create and assign a local number (or string) variable with a constant value because the function was prototyped with that data type, rather than pass the same constant number inline to the call. Could be worse.. I've seen this in [employed to write] software:

                            #define ZERO 0
                            #define ONE 1
                            #define TWO 2

                            :doh:

                            C Offline
                            C Offline
                            Chad3F
                            wrote on last edited by
                            #57

                            Just noticed a typo I made.. It should have said: "Now a smart compiler should probably warn about this." I guess spell check doesn't catch everything. :^)

                            1 Reply Last reply
                            0
                            • M Mark Starr

                              Yup: I don't know C# well enough. Agreed. And, I didn't capitalize the 'i' at the beginning of the next sentence.

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

                              This thread started with:

                              List employees = new List();
                              if (employees != null){
                              employees = GetEmployees();

                              There are layers of things wrong with just this bit of code. "employees = new ..." created a new object, it is impossible for "if (employees != null)" to return false. (Others have said memory critical operations might have problems, but that can't happen because if it did happen, the behavior of C# is to throw an error. In which case, "if" would never execute. THEN it executes "employees = GetEmployees()" which threw the "=..." part of "employees = new ..." into the trash. NOW employees may or may not be null and no if checking if it is null or not is done, it just assumes that since it wasn't null it still isn't null. It is incredible how much bad code can be compressed into so little space, so it is worth posting in the weird and wonderful. The following code has exactly the same amount of effective error checking as the first bit of code:

                              List employees = GetEmployees();

                              Sorry, I wasn't trying to bash you about your knowledge. (I was teasing you about your mistyping, thinking you wrote it while tired and made an easy mistake to make when you are tired.) The SQL part of what I said was just trying to teach you a little bit of the difference between the two languages. Everyone is ignorant in some things in their lives. Nothing wrong with that. There is something wrong with wanting to be ignorant and I didn't think that was happening with you.

                              M 1 Reply Last reply
                              0
                              • K KP Lee

                                This thread started with:

                                List employees = new List();
                                if (employees != null){
                                employees = GetEmployees();

                                There are layers of things wrong with just this bit of code. "employees = new ..." created a new object, it is impossible for "if (employees != null)" to return false. (Others have said memory critical operations might have problems, but that can't happen because if it did happen, the behavior of C# is to throw an error. In which case, "if" would never execute. THEN it executes "employees = GetEmployees()" which threw the "=..." part of "employees = new ..." into the trash. NOW employees may or may not be null and no if checking if it is null or not is done, it just assumes that since it wasn't null it still isn't null. It is incredible how much bad code can be compressed into so little space, so it is worth posting in the weird and wonderful. The following code has exactly the same amount of effective error checking as the first bit of code:

                                List employees = GetEmployees();

                                Sorry, I wasn't trying to bash you about your knowledge. (I was teasing you about your mistyping, thinking you wrote it while tired and made an easy mistake to make when you are tired.) The SQL part of what I said was just trying to teach you a little bit of the difference between the two languages. Everyone is ignorant in some things in their lives. Nothing wrong with that. There is something wrong with wanting to be ignorant and I didn't think that was happening with you.

                                M Offline
                                M Offline
                                Mark Starr
                                wrote on last edited by
                                #59

                                Not a prob, I was in fact, just starting the day, and clearly had too little coffee in my system to type, much less proof. We had just been playing around with javascript to see what happens when you don't declare a variable versus when you declare it and don't assign a value - how it affects checking for undefined, not-undefined, null, etc. And, Yes, I typically write a lot more in T-SQL, but I also use VB, VB.NET, et al in my daily work - hence my disclaimer about not writing a lot of C#. I'm always careful when checking for nulls, so when I see comparison operator my hair stands up a bit. Initially, to me, the code looked as though someone had somehow had a bad experience populating the list from using the "new List();" call, and so put in a safety-catch (if you will) to try again if necessary. Or, perhaps, the second part was already there, and someone later came along and added the first line... I'm sure they though it was right at the time. Who knows! Thanks for the note KP. Cheers and happy coding, Mark

                                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