Click here to Skip to main content
12,634,030 members (24,469 online)

Welcome to the Lounge

   

For discussing anything related to a software developer's life. Technical discussions are encouraged, but click here to ask your programming questions.

The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct, no programming questions and please don't post ads.
 
GeneralMethod chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 4:53
adminChris Maunder10-Jan-13 4:53 
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.

GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 5:00
memberDalek Dave10-Jan-13 5:00 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 5:07
adminChris Maunder10-Jan-13 5:07 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Norm .net10-Jan-13 5:14
groupNorm .net10-Jan-13 5:14 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Shelby Robertson 10-Jan-13 5:41
member Shelby Robertson 10-Jan-13 5:41 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Paul Conrad10-Jan-13 7:06
memberPaul Conrad10-Jan-13 7:06 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Norm .net10-Jan-13 21:20
groupNorm .net10-Jan-13 21:20 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 5:18
memberDalek Dave10-Jan-13 5:18 
JokeRe: Method chaining with short-circuit parameter evaluation Pin
djj5510-Jan-13 5:34
memberdjj5510-Jan-13 5:34 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
PIEBALDconsult10-Jan-13 5:01
memberPIEBALDconsult10-Jan-13 5:01 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Maximilien10-Jan-13 5:03
memberMaximilien10-Jan-13 5:03 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
peterchen10-Jan-13 5:06
memberpeterchen10-Jan-13 5:06 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 5:08
adminChris Maunder10-Jan-13 5:08 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
peterchen10-Jan-13 5:20
memberpeterchen10-Jan-13 5:20 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 5:26
memberDalek Dave10-Jan-13 5:26 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Nagy Vilmos10-Jan-13 5:34
memberNagy Vilmos10-Jan-13 5:34 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dalek Dave10-Jan-13 5:37
memberDalek Dave10-Jan-13 5:37 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Nagy Vilmos10-Jan-13 5:40
memberNagy Vilmos10-Jan-13 5:40 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
jibesh10-Jan-13 6:03
memberjibesh10-Jan-13 6:03 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
gavindon10-Jan-13 5:08
membergavindon10-Jan-13 5:08 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Espen Harlinn10-Jan-13 5:11
mvpEspen Harlinn10-Jan-13 5:11 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Eddy Vluggen10-Jan-13 5:53
mvpEddy Vluggen10-Jan-13 5:53 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Jörgen Andersson10-Jan-13 9:42
memberJörgen Andersson10-Jan-13 9:42 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Ranjan.D10-Jan-13 6:01
memberRanjan.D10-Jan-13 6:01 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Dave Kreskowiak10-Jan-13 5:13
mvpDave Kreskowiak10-Jan-13 5:13 
GeneralThere is a cool pattern Pin
Ennis Ray Lynch, Jr.10-Jan-13 5:22
memberEnnis Ray Lynch, Jr.10-Jan-13 5:22 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Pete O'Hanlon10-Jan-13 5:43
protectorPete O'Hanlon10-Jan-13 5:43 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 11:53
adminChris Maunder10-Jan-13 11:53 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Pete O'Hanlon10-Jan-13 11:58
protectorPete O'Hanlon10-Jan-13 11:58 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
mark merrens10-Jan-13 5:54
membermark merrens10-Jan-13 5:54 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Roger Wright10-Jan-13 6:02
memberRoger Wright10-Jan-13 6:02 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Nemanja Trifunovic10-Jan-13 6:11
memberNemanja Trifunovic10-Jan-13 6:11 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 11:54
adminChris Maunder10-Jan-13 11:54 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
AspDotNetDev10-Jan-13 6:15
protectorAspDotNetDev10-Jan-13 6:15 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
szukuro10-Jan-13 6:21
memberszukuro10-Jan-13 6:21 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
szukuro10-Jan-13 6:18
memberszukuro10-Jan-13 6:18 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Zac Greve10-Jan-13 6:41
memberZac Greve10-Jan-13 6:41 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
OriginalGriff10-Jan-13 6:19
mvpOriginalGriff10-Jan-13 6:19 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Andrew Rissing10-Jan-13 6:40
memberAndrew Rissing10-Jan-13 6:40 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Zac Greve10-Jan-13 6:40
memberZac Greve10-Jan-13 6:40 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Richard Deeming10-Jan-13 6:57
memberRichard Deeming10-Jan-13 6:57 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
AspDotNetDev10-Jan-13 7:31
protectorAspDotNetDev10-Jan-13 7:31 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Jörgen Andersson10-Jan-13 8:59
memberJörgen Andersson10-Jan-13 8:59 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Chris Maunder10-Jan-13 11:42
adminChris Maunder10-Jan-13 11:42 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
PHS24110-Jan-13 7:28
memberPHS24110-Jan-13 7:28 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
AspDotNetDev10-Jan-13 7:31
protectorAspDotNetDev10-Jan-13 7:31 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
PHS24110-Jan-13 9:50
memberPHS24110-Jan-13 9:50 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
Gary Wheeler10-Jan-13 8:14
memberGary Wheeler10-Jan-13 8:14 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
CodeHawkz10-Jan-13 21:13
memberCodeHawkz10-Jan-13 21:13 
GeneralRe: Method chaining with short-circuit parameter evaluation Pin
englebart11-Jan-13 3:18
memberenglebart11-Jan-13 3:18 

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.


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 9 Dec 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid