|
I am testing several depths of properties in an object to make sure they are safe before I call them:
Test.NotNull(myObject);
if (myObject != null)
{
Test.NotNull(myObject.MyProperty);
if (myObject.MyProperty != null)
Test.IsPositive(myObject.MyProperty.Id);
...
}
I was thinking it would be really cool to be able to do
Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id);
but obviously if myObject == null then we have a runtime null ref error because, regardless of what the NotNull method returns as part of the chaining, myObject is still null.
So this got me thinking: You can do
if (myObject != null && myObject.MyProperty != null)
because of short circuit boolean evaluation in C#, but I was wondering, with my fairly mainstream experience in languages, if there are languages out there that would allow chaining of methods with short circuit evaluation.
Essentially you'd have to have the input parameter be resolved after the method was called in order to have the method be able to say "I don't need the input parameter, please just ignore it".
Has anyone heard of this? Would it open up a World Of Pain when it comes to debugging? Would it be useful? Am I procrastinating?
---
Update: and it turns out this leads into a great discussion of extension methods. See
The Maybe Monad[^] and Chained null checks and the Maybe monad[^] for two ways of achieving this. Once you've done that, debate the correctness of extension methods that are able by design to operate on a null references.
I will be over there looking for new, shiny, distracting things.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
modified 10-Jan-13 16:48pm.
|
|
|
|
|
No Programming Questions in the lounge.
I am going to have to report you to yourself.
Please give yourself a good dressing down and wag an admonishing finger at yourself!
---------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
|
|
|
|
|
I would if it were a programming question.
And, regardless of the attempt at humour, I am sad, deeply sad, that it was you who jumped on the "no programming questions" bandwagon.
So I'm going to jump on my soapbox because it's a fantastic way to procrastinate further, and bemoan the lack of interesting technical discussion on the lounge where bad jokes and political rhetoric seem to be more accepted than discussion on the topics that we as developers hold dearest to our hearts.
For shame, David. For shame.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
If voting was available, I would of shot you a 5, but alas.....
|
|
|
|
|
Norm .net wrote: If voting was available, I would of shot you a 5, but alas.....
Seconded.
CPallini wrote: You cannot argue with agile people so just take the extreme approach and shoot him.
:Smile:
|
|
|
|
|
You could just toss a in place of a 5 vote.
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
Aye
|
|
|
|
|
Do Not Procrastinate Today!
Wait until tomorrow and have a whole day of it.
Until then, stare at this picture[^] of a cat.
---------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
|
|
|
|
|
I think you should ban him!
|
|
|
|
|
Oooh! Oooh! Oooh! Use ON ERROR RESUME !
|
|
|
|
|
Chris Maunder wrote: Am I <layer>procrastinating?
Yes.
Nihil obstat
|
|
|
|
|
For years I wished for something like
<br />
Flibber f = Bugger?.GetGoodShoboil()?.Libbl;<br />
to evaluate to null silently if either Bugger is or GetGoodShoboil Returns null.
I dimly remember last time I voiced this here, I was told to not want this by the disciples of the Occasionally Useful Suggestion Of Demeter[^], as well as an apparently unrelated Sir Or Madam Chris Munder who suggested to not add more garbage to this wonderful, sleek new thing called C#.
|
|
|
|
|
I was drunk.
That suggestion is awesome and I recant.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Chris Maunder wrote: I was drunk.
You always say that
|
|
|
|
|
He's always drunk.
---------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
|
|
|
|
|
No. I always say that.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
You're always drunk!
---------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
|
|
|
|
|
I may well be tonight.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
so who is really drunk here. was it me?
Jibesh V P
|
|
|
|
|
Chris Maunder wrote: Am I procrastinating?
I think this would get my vote...
Beauty is in the eye of the beer-holder
Be careful which toes you step on today, they might be connected to the foot that kicks your butt tomorrow.
You can't scare me, I have children.
|
|
|
|
|
|
Or a decorator, preferably combined with the NullObject-pattern
|
|
|
|
|
System.DBNull is an example of the pattern. And I can't describe in words how much I dislike it.
People say nothing is impossible, but I do nothing every day.
|
|
|
|
|
Yes my vote for extension methods
Ranjan.D
|
|
|
|
|
Interesting.
Today, you'd have to wrap it in a Try/Catch and eat the null reference exception if thrown.
But, if you could do this, you would have to be able to tell the compiler you don't want the exception thrown and to just continue with the next statement.
Hell, they added async/await keywords, why not add another one?
I'm kind of thinking that this would complicate debugging a bit too.
|
|
|
|