What is null equal to?
-
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
Eh, not my choice. Code style rules...
-
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)) {
Which can be simplified to
if (null==value)
{
throw new NullPointerException();//It was null, we don't like their kind here
}
else
{
//Do something
} -
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)
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.
-
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
-
Which can be simplified to
if (null==value)
{
throw new NullPointerException();//It was null, we don't like their kind here
}
else
{
//Do something
}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[^]
-
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[^]
Woops, logic derp. Yep, you're right. The code is Java for context, but it does lazy logic evaluation too...
-
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[^]
Taking the code from Vark111's post, and assuming C#:
if (null == value || (null).Equals(value)) {
If
value
isn'tnull
, the second part will be evaluated. Since the second part tries to call theEquals
method on a null reference, it would throw aNullReferenceException
. :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
-
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.
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.
-
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.
“==” 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
-
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.
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.
-
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)
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.