Return the correct null
-
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return null;
}Oh no, how come you thought you could return
something
when it isnull
? That's a differentnull
! -
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return null;
}Oh no, how come you thought you could return
something
when it isnull
? That's a differentnull
!Saw similar in javascript last week:
function checkIfValid() {
var isValid = false;
if (doSomeOtherCheck()) {
isValid = true;
}...
if (isValid == true) {
return true;
} else {
return false;
}
}[Edit] Updated to show more content from the function.
Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.
-
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return null;
}Oh no, how come you thought you could return
something
when it isnull
? That's a differentnull
! -
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return null;
}Oh no, how come you thought you could return
something
when it isnull
? That's a differentnull
! -
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return null;
}Oh no, how come you thought you could return
something
when it isnull
? That's a differentnull
!Actually they are different things, kind of. The FirstOrDefault is going to return default(ISomething) Your assertion that "something" is going to be null is actually an assumption. The code would indeed be redundant if it was this
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return default(ISomething);
}however we're getting into new levels of pedantry here :D I sometimes write things like in the OP if I want to make it explicitly clear that it is expected that the function can return a null value.
-
Saw similar in javascript last week:
function checkIfValid() {
var isValid = false;
if (doSomeOtherCheck()) {
isValid = true;
}...
if (isValid == true) {
return true;
} else {
return false;
}
}[Edit] Updated to show more content from the function.
Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.
That makes sense if valid is "true" (string) or 1 (int), but you want to return a boolean :)
Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly
-
Saw similar in javascript last week:
function checkIfValid() {
var isValid = false;
if (doSomeOtherCheck()) {
isValid = true;
}...
if (isValid == true) {
return true;
} else {
return false;
}
}[Edit] Updated to show more content from the function.
Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.
That almost makes sense in Javascript: Truthy and Falsy: When All is Not Equal in JavaScript[^] Although you could shorten it to:
return !!isValid;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Actually they are different things, kind of. The FirstOrDefault is going to return default(ISomething) Your assertion that "something" is going to be null is actually an assumption. The code would indeed be redundant if it was this
ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
if (something != null)
{
return something;
}
else
{
return default(ISomething);
}however we're getting into new levels of pedantry here :D I sometimes write things like in the OP if I want to make it explicitly clear that it is expected that the function can return a null value.
F-ES Sitecore wrote:
The FirstOrDefault is going to return default(ISomething)
Not necessarily. Assuming
ISomething
is an interface,default(ISomething)
will benull
. But:interface ISomething { }
struct Something : ISomething { }var listOfSomethings = new List<Something>();
ISomething result = listOfSomethings.FirstOrDefault();
Console.WriteLine(result == null); // FalseFor an empty sequence,
FirstOrDefault
returnsdefault(TSource)
, whereTSource
is the type parameter of the input sequence. And ifISomething
isn't an interface, then whoever wrote the code needs to be introduced to the clue-bat. Now that's a new level of pedantry! :-D
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
That makes sense if valid is "true" (string) or 1 (int), but you want to return a boolean :)
Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly
I should have wrote that further up the function, isValid is set to either true or false :)
Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.