|
Reading some of the posts on this forum reminds me of a piece of C++ code written many years ago which went something like:
Class MyClass
{
public:
~MyClass()
{
delete this;
}
}
Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
|
|
|
|
|
See my sig.
Software Zen: delete this;
|
|
|
|
|
I come before you with a question, a yearning in my heart. What <dramatic pause=""> 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.
|
|
|
|
|
What is the sound of two nulls equating?
|
|
|
|
|
Silence from Lorrha [^]
[Update} Well, that's pretty useless: the audio track doesn't work.
Or maybe that's the point: literally the sound of nothing at all: null in the truest sense.
|
|
|
|
|
In programming a null object means that this doesn't exist in the memory. For example,
int i;
if(i == null) {
MessageBox.Show("Yes, Null!");
} else {
MessageBox.Show("No, value was added somewhere in the code stack");
}
This, would execute if there is no value in i , or the i was never initialized. The code that you're having is something like this
if (value == null || value.equals(null)) {
return "null";
}
The method signature would be like,
public string Function1 () {
}
Favourite line: Throw me to them wolves and close the gate up. I am afraid of what will happen to them wolves - Eminem
~! Firewall !~
modified 25-Aug-14 13:29pm.
|
|
|
|
|
I understand the concept of null, however, evidently "those who came before" didn't...
Hence why I'm posting this here
|
|
|
|
|
|
OriginalGriff wrote: But you'd have to be a complete moron to write that!, and think that it is good code!
Oops. I just did...
FTFY!
What do you get when you cross a joke with a rhetorical question?
---
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
|
|
|
|
|
Mmmh, yeah, there's stuff like that in there too. I'll have to see if I can anonymize it enough to be internet postable. Let's just say someone did not understand object oriented coding and how Java works. We got to use this library before we actually took over developing it as contractors, and all the problems we had with it before are starting to make sense now...
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
The problem is not the result of "null", the problem is the value.equals(null) .
The first part of the if is right (value == null). The second is wrong and it would actually never execute when the value is null (and when it does execute, value will not be null, avoiding an exception).
|
|
|
|
|
euhm, isn't
int i;
never null (it can't be null, that's where int? i is for )
(in C# that is)
(just double checked, you cannot even compile if not initialized, in the immediate window it initialized to 0)
|
|
|
|
|
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();
}
else
{
}
|
|
|
|
|
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
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...
|
|
|
|
|
Taking the code from Vark111's post, and assuming C#:
if (null == value || (null).Equals(value)) {
If value isn't null , the second part will be evaluated. Since the second part tries to call the Equals method on a null reference, it would throw a NullReferenceException .
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
|
|
|
|
|
sloosecannon wrote: What <dramatic pause="">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...
|
|
|
|
|
Nothing wrong with that. It's very common.
K&R Style[^]
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
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.
|
|
|
|
|
“==” 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
|
|
|
|
|
sloosecannon wrote: What is null equal to?
The place I worked in Germany?
|
|
|
|