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 2 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.
  • OriginalGriffO OriginalGriff

    Chris Maunder wrote:

    Am I procrastinating?

    Yep! But then, you probably don't want to fix RootAdmin - it's down according to http://www.downforeveryoneorjustme.com/rootadmin.com[^]

    If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

    A Offline
    A Offline
    Andrew Rissing
    wrote on last edited by
    #33

    Maybe this IS the fix for the site.

    1 Reply Last reply
    0
    • S szukuro

      In C# you can implement this k<ind of chaining using extension methods and expressions trees. The extension method (somewhat combined version of the two above):

          public static U NotNull(this T myObject, Expression expression) 
              where U : class
          {
              if (myObject == null)
                  return null;
              else
              {
                 var func = expression.Compile();
                  return func();
              }
          }
      

      Usage (returns either null if there's a null in the chain, or the value of MySubProperty, with almost the same syntax as above):

      var value = myObject.NotNull(() => myObject.MyProperty).NotNull(() => myProperty.MySubProperty);

      The only downside I found that in this case you have to declare a variable of type MyProperty for use in the second lambda expression. Maybe there's a way around it, but I didn't manage to find one as of yet. That can be changed though if the input remains myObject and the expression consists of the full path i. e. () => myObject.MyProperty.MySubProperty.

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #34

      I figured out something that works. See my message below. It is based off of yours, with an extra parameter for the expression.

      Bob Dole

      The internet is a great way to get on the net.

      :doh: 2.0.82.7292 SP6a

      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

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #35

        If only there was some sort of site where people could write articles on exactly this issue[^]! ;P


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        A J C 3 Replies Last reply
        0
        • N NormDroid

          If voting was available, I would of shot you a 5, but alas.....

          Software Kinetics - Dependable Software news

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #36

          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 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
            Septimus Hedgehog
            wrote on last edited by
            #37

            Have you looked at this CP article by Dimitri Nesteruk[^] It looks very useful.

            "I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68). "I don't need to shoot my enemies, I don't have any." - Me (2012).

            A 1 Reply Last reply
            0
            • S Septimus Hedgehog

              Have you looked at this CP article by Dimitri Nesteruk[^] It looks very useful.

              "I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68). "I don't need to shoot my enemies, I don't have any." - Me (2012).

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #38

              You were beat to the punch. :)

              Thou mewling ill-breeding pignut!

              S 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                If only there was some sort of site where people could write articles on exactly this issue[^]! ;P


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #39

                Yes! :thumbsup:

                Thou mewling ill-breeding pignut!

                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

                  G Offline
                  G Offline
                  Gary Wheeler
                  wrote on last edited by
                  #40

                  In C++ you could do this with an 'instance' call like this:

                  if (this != NULL)
                  {
                  return this->real_stuff;
                  }
                  else
                  {
                  return default_stuff;
                  }

                  As others have mentioned, in C# you can do this with extension methods:

                  namespace Test
                  {
                  public class Widget
                  {
                  }
                  public static class WidgetExtensions
                  {
                  static public void Method(this Widget _this)
                  {
                  if (_this != null)
                  {
                  Console.WriteLine("real widget");
                  }
                  else
                  {
                  Console.WriteLine("null widget");
                  }
                  }
                  }
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  Widget widget = null;

                          widget.Method();
                          
                          widget = new Widget();
                          
                          widget.Method();
                      }
                  }
                  

                  }

                  results in the output

                  null widget
                  real widget

                  Software Zen: delete this;

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    If only there was some sort of site where people could write articles on exactly this issue[^]! ;P


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    J Offline
                    J Offline
                    Jorgen Andersson
                    wrote on last edited by
                    #41

                    That settles it for me, I want voting back. :thumbsup:

                    People say nothing is impossible, but I do nothing every day.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Or a decorator, preferably combined with the NullObject-pattern :)

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #42

                      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.

                      1 Reply Last reply
                      0
                      • A AspDotNetDev

                        You were beat to the punch. :)

                        Thou mewling ill-breeding pignut!

                        S Offline
                        S Offline
                        Septimus Hedgehog
                        wrote on last edited by
                        #43

                        So I was. Great minds think alike but in my case I'll pass up on the citation. ;) Hopefully it'll convince Muppet Maunder to read it and see if helps him.

                        "I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68). "I don't need to shoot my enemies, I don't have any." - Me (2012).

                        1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          If only there was some sort of site where people could write articles on exactly this issue[^]! ;P


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                          I am humbled and ashamed. And have just found an endless opportunity for way, way more procrastination.

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

                          1 Reply Last reply
                          0
                          • P Pete OHanlon

                            There is a way that you could do this, but it would be much more trouble than it was worth, and it would definitely open up a whole world of hurt. Effectively, what would need to be implemented is an IL rewriter to manage and rewrite the chain internally. Then you could create a fluid interface that would need to be recognised as the item that needs rewriting.

                            *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

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

                            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 1 Reply Last reply
                            0
                            • 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
                                          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