|
The guy’s an idiot. You do what’s appropriate for the situation. If a null return value is warranted, then by the hounds of hell, return a f*ckin null.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Software Zen: delete this;
|
|
|
|
|
I seem to write a lot of methods that return an IEnumerable<T> where I use 'yield return' ... what else would I use when there are no T's to return ? default(T) is going to fail, or give me an instance.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
|
Thanks, but,
using: yield return Enumerable.Empty<T>
would require the return type of the Function to be:
IEnumerable<IEnumerable<T>>
and, in the context of Linq enumeration, return without yield will result in an error.
And, that would mean you could not use yield return someinstanceofT; Keep in mind the context here is an Extension method using Linq enumeration to return an IEnumerable<T>
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Right you are, think I was thinking about standard functions rather than ones that yield.
|
|
|
|
|
Either yield break; , or just return from the method without issuing any yield return ...; statements.
The caller will get back a non-null IEnumerable<T> instance which produces no elements.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard, while the result of 'yield return null' and 'yield break' are the same, and using 'break saves typing 6 characters ... somehow I find yielding 'null more ... uhhh ... mnemonic.
Richard Deeming wrote: return from the method without issuing any yield return given that I expect these methods to return stuff most of the time, there has to be some use of the 'yield statement, or you are going to hear the old song: "not all code paths ..." maybe I am missing the point you are making ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
BillWoodruff wrote: the result of 'yield return null' and 'yield break' are the same
But they're not though.
static IEnumerable<string> YieldReturnNull()
{
yield return null;
}
static IEnumerable<string> YieldBreak()
{
yield break;
}
static void Main()
{
Console.WriteLine("Yield return null:");
foreach (string item in YieldReturnNull())
{
Console.WriteLine("Item: {0}", item);
}
Console.WriteLine();
Console.WriteLine("Yield break:");
foreach (string item in YieldBreak())
{
Console.WriteLine("Item: {0}", item);
}
Console.WriteLine();
} Yielding | C# Online Compiler | .NET Fiddle[^]
Output:
Yield return null:
Item:
Yield break: yield return null; creates an enumerator which returns a single item. yield break; creates an enumerator which doesn't return any items.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
@RichardDeeming
Hi Richard, Thanks for (as always) enlightening !
I will respond to this in depth with a post on the C# forum ... in a few days ... that I hope might spark a "best practices" discussion. I went back and searched my code for the last year for instances of 'yield break' ... I have been using it, along with the 'other.'
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I return nulls a lot. I think twice before I don't check for nulls. Still I do that less often than some people.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
i've totally stopped returning NULL. i'm writing Go. it uses 'nil' instead.
|
|
|
|
|
Mission accomplished!
TTFN - Kent
|
|
|
|
|
The author of that article should read up a good book on Functional Programming to learn better options to what he described.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Lol, returning a default value, like in his example a connection string to the wrong database, yeah right
modified 20-Oct-19 21:02pm.
|
|
|
|
|
With its well-thought-out rebuild of Dart 2 five years later, Google seemed to have proven its commitment to the language. Indeed, today it continues to gain traction among developers—especially Java and C# veterans. She makes a few good points
Let the punnage revenge flow over me now - like sticky mushroom gravy (that part wasn't a pun, I'm not a fan of gravy)
|
|
|
|
|
|
A-wow, what a square, don't you dig the scene?
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
This is going to be possibly insensitive but was that author ALWAYS a she? If I get a hand slap about this so be it.
|
|
|
|
|
What would you do differently at work if you were the boss? I'm really surprised that's not 100%
|
|
|
|
|
Not me. Have had the same laptop for 6 years. If I get a faster machine, I "might" become more productive. Then my boss would expect that all the time. Keep the expectations low and use technology as the scapegoat. Win-win.
|
|
|
|
|
OK, that makes perfect sense. I sit corrected.
TTFN - Kent
|
|
|
|
|
Let your workplace be upgraded. You might always blame the IT, they can slow you down even more than old technology.
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
You are a fan of Wally, aren't you?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Every week, I politely ask "Where the F**K is my helicopter?!?", but no-one ever has an acceptable answer.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|