|
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?
|
|
|
|
|
I was asked to make small amendments to an ages old ASP Classic website. So I tried to log into the "administration" area, didn't know what username/password to use, and opened up the code to see where in the database (MSAccess) I should look for valid credentials...
Behold (some details left out/altered to protect involved parties):
Dim msg
msg=""
Dim sql
sql="SELECT * FROM USERS WHERE (usr= '" + username +"')"
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = dbconnSTRING
rs.Source = sql
rs.CursorType = 0
rs.CursorLocation = 2
rs.Open()
if rs.Eof And rs.Bof then
msg="Invalid username"
end if
sql="SELECT * FROM USERS WHERE (pswd= '"+ password +"')"
rs.Close()
rs.Open(sql)
if rs.Eof And rs.Bof then
if msg="Invalid username" then
msg="Invalid username and password"
else
msg="Invalid password"
end if
end if
So basically if I know your username and I have my own account, I can use your username and my password and log in as you...
Nice eh?
Φευ! Εδόμεθα υπό ρηννοσχήμων λύκων!
(Alas! We're devoured by lamb-guised wolves!)
|
|
|
|
|
|
A thing of beauty. Thanks for immortalizing it here.
P.S. I'll also take a moment to point out that such a validation routine should never indicate what went wrong, only that it failed. Telling a potential baddy that the user name doesn't exist makes his job easier -- he's simply stop trying that username and move on to the next without trying any more passwords.
modified 25-Aug-14 11:30am.
|
|
|
|
|
I'm never sure about that one. Yes, it has a marginal effect on security, but it has a big effect on user annoyance, and I think the trade-off is worth it in most cases to let a user know that they mistyped their username.
|
|
|
|
|
Airtight 
|
|
|
|
|
Bah, I don't need a valid username OR a valid password...
Username: 'or''='
Password: 'or''='
I know, I know... I'm supposed to drop/wipe the table, but that's just mean.
|
|
|
|
|
Nah, username and password were sanitized earlier in the code. Surprisingly, the sanitization routine is pretty solid (probably copy-pasted from elsewhere though, seems quite out-of-place in terms of coding style).
Φευ! Εδόμεθα υπό ρηννοσχήμων λύκων!
(Alas! We're devoured by lamb-guised wolves!)
|
|
|
|
|
I think the lesson you can derive from this is to teach the developer who wrote this what the AND keyword means in SQL syntax.
|
|
|
|
|
Actually, the person who originally wrote this little gem currently has something close to 25 years of active development under their belt, with extensive SQL work as well. I've seen other samples of their work, written about the same time as this, and they are REALLY better than this. So this leads me to think that they were smoking something REALLY good when they wrote this.
Φευ! Εδόμεθα υπό ρηννοσχήμων λύκων!
(Alas! We're devoured by lamb-guised wolves!)
|
|
|
|
|
|
Yeah, that might be the case . But not anymore
Φευ! Εδόμεθα υπό ρηννοσχήμων λύκων!
(Alas! We're devoured by lamb-guised wolves!)
|
|
|
|
|
Code from the time that Sex was safe and flying dangerous 
|
|
|
|
|
Well, flying is still somewhat dangerous...
Φευ! Εδόμεθα υπό ρηννοσχήμων λύκων!
(Alas! We're devoured by lamb-guised wolves!)
|
|
|
|
|
Am I the last one to know that the Android SDK defines a constant[^] for the gravity on the first Death Star? Just noticed it today. I wonder whether that is the surface gravity. The gravity inside looked much higher (since people walked normally).
There also are constants for other (real) planets.
|
|
|
|
|
public static final float GRAVITY_DEATH_STAR_I Added in API level 1
Gravity (estimate) on the first Death Star in Empire units (m/s^2)
Constant Value: 3.5303614E-7
Also interesting to note that the Empire now uses metric (SI) units.
And where is the island which has about half of Earth's gravity?
|
|
|
|
|