Click here to Skip to main content
15,891,607 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralMethod chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 3:53
cofounderChris Maunder10-Jan-13 3:53 
I am testing several depths of properties in an object to make sure they are safe before I call them:
C#
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
C#
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
C#
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.

GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 4:00
professionalDalek Dave10-Jan-13 4:00 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 4:07
cofounderChris Maunder10-Jan-13 4:07 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
NormDroid10-Jan-13 4:14
professionalNormDroid10-Jan-13 4:14 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Shelby Robertson 10-Jan-13 4:41
Shelby Robertson 10-Jan-13 4:41 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Paul Conrad10-Jan-13 6:06
professionalPaul Conrad10-Jan-13 6:06 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
NormDroid10-Jan-13 20:20
professionalNormDroid10-Jan-13 20:20 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 4:18
professionalDalek Dave10-Jan-13 4:18 
JokeRe: Method chaining with short-circuit parameter evaluation Pin
Corporal Agarn10-Jan-13 4:34
professionalCorporal Agarn10-Jan-13 4:34 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
PIEBALDconsult10-Jan-13 4:01
mvePIEBALDconsult10-Jan-13 4:01 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Maximilien10-Jan-13 4:03
Maximilien10-Jan-13 4:03 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
peterchen10-Jan-13 4:06
peterchen10-Jan-13 4:06 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 4:08
cofounderChris Maunder10-Jan-13 4:08 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
peterchen10-Jan-13 4:20
peterchen10-Jan-13 4:20 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 4:26
professionalDalek Dave10-Jan-13 4:26 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Nagy Vilmos10-Jan-13 4:34
professionalNagy Vilmos10-Jan-13 4:34 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 4:37
professionalDalek Dave10-Jan-13 4:37 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Nagy Vilmos10-Jan-13 4:40
professionalNagy Vilmos10-Jan-13 4:40 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Jibesh10-Jan-13 5:03
professionalJibesh10-Jan-13 5:03 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
gavindon10-Jan-13 4:08
gavindon10-Jan-13 4:08 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Espen Harlinn10-Jan-13 4:11
professionalEspen Harlinn10-Jan-13 4:11 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Eddy Vluggen10-Jan-13 4:53
professionalEddy Vluggen10-Jan-13 4:53 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Jörgen Andersson10-Jan-13 8:42
professionalJörgen Andersson10-Jan-13 8:42 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Ranjan.D10-Jan-13 5:01
professionalRanjan.D10-Jan-13 5:01 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dave Kreskowiak10-Jan-13 4:13
mveDave Kreskowiak10-Jan-13 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.