What this 'null' check doing here...
-
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.
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.
-
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.
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.
-
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.
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...
-
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.
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.
-
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.
He is just double checking. :)
-
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?"
-
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.
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?
-
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 -
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.
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.
-
Probably code that has been edited and then not refactored. No real wtf here, ey?
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.
-
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...
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.
-
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.
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.
-
He is just double checking. :)
: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.
-
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?
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.
-
:)
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.
-
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.
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...)
-
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.
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.
-
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.
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
-
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.
lol. Then its probably just a brain fart, or late night coding session :)
-
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.
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).