Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Method chaining with short-circuit parameter evaluation

Method chaining with short-circuit parameter evaluation

Scheduled Pinned Locked Moved The Lounge
csharpc++phpcomdesign
59 Posts 38 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nemanja Trifunovic

    Out of curiosity, what would you do in the else section of such a call?

    utf8-cpp

    C Offline
    C Offline
    Chris Maunder
    wrote on last edited by
    #46

    Else? There is no else. Just blindly plow on!

    cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

    1 Reply Last reply
    0
    • C Chris Maunder

      I respect a man who prefers power drills and small explosives where others would wimp out and use tweezers. :thumbsup:

      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #47

      When I get the chance, I may just have a go at it. Need some free time first.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      1 Reply Last reply
      0
      • C Chris Maunder

        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

        C Offline
        C Offline
        CodeHawkz
        wrote on last edited by
        #48

        The first code block doesn't make any sense to me but this line

        var result = Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id);

        can be represented like this, can't you?

        var result = (myObject == null ? false: myObject.MyProperty == null ? false : IsPositive(myObject.MyProperty.Id);

        Just bouncing off an idea :) P.S. You can do it much simpler by using exceptions, I believe Cheers

        1 Reply Last reply
        0
        • P Paul Conrad

          You could just toss a :thumbsup: in place of a 5 vote.

          "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

          N Offline
          N Offline
          NormDroid
          wrote on last edited by
          #49

          Aye :thumbsup:

          Software Kinetics - Dependable Software news

          1 Reply Last reply
          0
          • C Chris Maunder

            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

            E Offline
            E Offline
            englebart
            wrote on last edited by
            #50

            Would the C# ?? operator help you?
            Would that do what you want? It definitely saves typing.
            http://msdn.microsoft.com/en-us/library/ms173224.aspx

            1 Reply Last reply
            0
            • C Chris Maunder

              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

              C Offline
              C Offline
              Cesar de Souza
              wrote on last edited by
              #51

              Not really an answer for the question you asked, but just a comment about error handling. A good strategy to avoid endless nested checks in error handling is to handle the opposite case first. So instead of writing

              Test.NotNull(myObject);
              if (myObject != null)
              {
              Test.NotNull(myObject.MyProperty);
              if (myObject.MyProperty != null)
              Test.IsPositive(myObject.MyProperty.Id);
              ...
              }

              we would have

              Test.NotNull(myObject);
              if (myObject == null)
              return;

              Test.NotNull(myObject.MyProperty)
              if (myObject.MyProperty == null)
              return;

              Test.IsPositive(myObject.MyProperty.Id);

              ...

              Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Haar-feature Object Detection (With The Viola-Jones Framework) in C#

              C 1 Reply Last reply
              0
              • C Chris Maunder

                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

                S Offline
                S Offline
                Super Lloyd
                wrote on last edited by
                #52

                Chris Maunder wrote:

                I was thinking it would be really cool to be able to do

                Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id);

                You can do that with extension methods! Just saying! ;)

                My programming get away... The Blog... Taking over the world since 1371!

                1 Reply Last reply
                0
                • C Chris Maunder

                  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

                  F Offline
                  F Offline
                  Fabio Franco
                  wrote on last edited by
                  #53

                  Chris Maunder wrote:

                  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.

                  Well, I find myself doing a lot of those kind of checks and it would save me a lot of time and potential bugs if there was such a language. But now you have just stimulated me to think about a general purpose solution in C#. Maybe if I find something I'll post it on tips and tricks.

                  To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                  1 Reply Last reply
                  0
                  • C Cesar de Souza

                    Not really an answer for the question you asked, but just a comment about error handling. A good strategy to avoid endless nested checks in error handling is to handle the opposite case first. So instead of writing

                    Test.NotNull(myObject);
                    if (myObject != null)
                    {
                    Test.NotNull(myObject.MyProperty);
                    if (myObject.MyProperty != null)
                    Test.IsPositive(myObject.MyProperty.Id);
                    ...
                    }

                    we would have

                    Test.NotNull(myObject);
                    if (myObject == null)
                    return;

                    Test.NotNull(myObject.MyProperty)
                    if (myObject.MyProperty == null)
                    return;

                    Test.IsPositive(myObject.MyProperty.Id);

                    ...

                    Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Haar-feature Object Detection (With The Viola-Jones Framework) in C#

                    C Offline
                    C Offline
                    Chris Maunder
                    wrote on last edited by
                    #54

                    Not sure how that counts as handling the opposite case first, but I see what you mean and I do do that, though I do try and have only 2 exit points in a routine: 1 at the top when input fails, and the return at the end. Hence my thought that it would be nice having a single chained line at the top. All good discussion though. I love it.

                    cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                    C 1 Reply Last reply
                    0
                    • C Chris Maunder

                      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

                      A Offline
                      A Offline
                      Alan Balkany
                      wrote on last edited by
                      #55

                      It might be simpler to just push the test down into a function call. This keeps the calling method simple, making the code more understandable and maintainable. Anyone maintaining the code doesn't have to reason about a unique control structure, and can skip all the details to focus on the overall algorithm. If they're curious about the test, they can optionally browse the testing function. The chaining looks cool, but is more complex than a sequence of plain, vanilla, if statements.

                      1 Reply Last reply
                      0
                      • C Chris Maunder

                        Not sure how that counts as handling the opposite case first, but I see what you mean and I do do that, though I do try and have only 2 exit points in a routine: 1 at the top when input fails, and the return at the end. Hence my thought that it would be nice having a single chained line at the top. All good discussion though. I love it.

                        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                        C Offline
                        C Offline
                        Cesar de Souza
                        wrote on last edited by
                        #56

                        Sorry, I should have meant "handle the error case first and exit as soon as possible", even if this "as soon as possible" will only occur in the middle of the function. Usually I don't have problems with multiple exit points in a function. Those are particularly useful, for example, when implementing a syntax parser from a LL(1) grammar. Another situation may happen, for example, if you are in the middle of a very nested loop and would like to exit them all. Refactoring the loop into its own function and using a return is often one of the clearest ways to quit all loops at once, specially in comparison with the alternative which is to maintain quite a number of done variables guarding the loop exits as in

                        for (int i = 0; i < n && !done; i++)

                        By the way, another sensible alternative for this case is also to use a goto statement, this being one of the few justified cases for using it. I know this doesn't have anything to do with your question either, but I am just keeping up the discussion :)

                        Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Haar-feature Object Detection (With The Viola-Jones Framework) in C#

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          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

                          U Offline
                          U Offline
                          User 3314745
                          wrote on last edited by
                          #57

                          Actually, there is something like that. It's called Embarcadero Prism (ex Delphi Prism). Borland made Delphi Prism using the Oxygene language (ex Chrome) through some agreement with RemObjects Software. Borland needed it as a .NET language replacement for the trully crappy Borland Delphi.NET. Check this beauty out: http://wiki.oxygenelanguage.com/en/Oxygene\_by\_Example\_-\_Colon\_and\_Null

                          1 Reply Last reply
                          0
                          • C Chris Maunder

                            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

                            M Offline
                            M Offline
                            Member 4608898
                            wrote on last edited by
                            #58

                            Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id);

                            You can't possibly call this cool. This is a code janitor's nightmare. If any part of that fails, you don't know what has failed so you end up breaking it down into

                            aa = Test.NotNull(myObject);
                            bb = aa.NotNull(myObject.MyProperty)
                            cc = bb.IsPositive(myObject.MyProperty.Id);

                            to see what is falling over. Cool is only for those who make a mess then disappear off to another job and leave someone else to tidy up their mess. They've never had to maintain the crap they generate and they find it difficult to understand, let alone maintain someone else's mess. This means that you have to hire an expensive janitor to unravel the coolness. You really don't want cool - you want maintainability so you can hire a cheap code janitor (fresh graddy).

                            C 1 Reply Last reply
                            0
                            • M Member 4608898

                              Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id);

                              You can't possibly call this cool. This is a code janitor's nightmare. If any part of that fails, you don't know what has failed so you end up breaking it down into

                              aa = Test.NotNull(myObject);
                              bb = aa.NotNull(myObject.MyProperty)
                              cc = bb.IsPositive(myObject.MyProperty.Id);

                              to see what is falling over. Cool is only for those who make a mess then disappear off to another job and leave someone else to tidy up their mess. They've never had to maintain the crap they generate and they find it difficult to understand, let alone maintain someone else's mess. This means that you have to hire an expensive janitor to unravel the coolness. You really don't want cool - you want maintainability so you can hire a cheap code janitor (fresh graddy).

                              C Offline
                              C Offline
                              Chris Maunder
                              wrote on last edited by
                              #59

                              :| I was discussing a concept, not presenting production code.

                              cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups