Best way to check the null for combination of two object












1














I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.



For example : I have one function like this



int Function(Object* obj1, Object* obj2) 
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;


As we can see, we have too many if condition just to check the combination of two object for null.



Is there any other way to do it more effectively so that the readability is good?



Note : Object is a class and no operator is overloaded.










share|improve this question


















  • 1




    Your code is clear and readable to me.
    – Jabberwocky
    Nov 16 at 10:05






  • 2




    Object being a class object or not isn't relevant here.
    – Jabberwocky
    Nov 16 at 10:06






  • 1




    You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
    – felix
    Nov 16 at 10:21






  • 1




    Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
    – Yksisarvinen
    Nov 16 at 10:26






  • 1




    Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
    – tunglt
    Nov 16 at 10:30
















1














I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.



For example : I have one function like this



int Function(Object* obj1, Object* obj2) 
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;


As we can see, we have too many if condition just to check the combination of two object for null.



Is there any other way to do it more effectively so that the readability is good?



Note : Object is a class and no operator is overloaded.










share|improve this question


















  • 1




    Your code is clear and readable to me.
    – Jabberwocky
    Nov 16 at 10:05






  • 2




    Object being a class object or not isn't relevant here.
    – Jabberwocky
    Nov 16 at 10:06






  • 1




    You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
    – felix
    Nov 16 at 10:21






  • 1




    Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
    – Yksisarvinen
    Nov 16 at 10:26






  • 1




    Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
    – tunglt
    Nov 16 at 10:30














1












1








1







I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.



For example : I have one function like this



int Function(Object* obj1, Object* obj2) 
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;


As we can see, we have too many if condition just to check the combination of two object for null.



Is there any other way to do it more effectively so that the readability is good?



Note : Object is a class and no operator is overloaded.










share|improve this question













I wanted to ask this question since long time. After spending some time I feel like not getting a proper way to check two objects for null together, so i am asking it.



For example : I have one function like this



int Function(Object* obj1, Object* obj2) 
{
if(obj1 == null && obj2 == null)
{
// Do someting........
}
else if(obj1 == null && obj2 != null)
{
// Do something.....
}
else if(obj1 != null && obj2 == null)
{
// Do something......
}
else
{
// Do something........
}
return 0;


As we can see, we have too many if condition just to check the combination of two object for null.



Is there any other way to do it more effectively so that the readability is good?



Note : Object is a class and no operator is overloaded.







c++ coding-style






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 at 10:01









Nihal Kumar

528




528








  • 1




    Your code is clear and readable to me.
    – Jabberwocky
    Nov 16 at 10:05






  • 2




    Object being a class object or not isn't relevant here.
    – Jabberwocky
    Nov 16 at 10:06






  • 1




    You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
    – felix
    Nov 16 at 10:21






  • 1




    Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
    – Yksisarvinen
    Nov 16 at 10:26






  • 1




    Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
    – tunglt
    Nov 16 at 10:30














  • 1




    Your code is clear and readable to me.
    – Jabberwocky
    Nov 16 at 10:05






  • 2




    Object being a class object or not isn't relevant here.
    – Jabberwocky
    Nov 16 at 10:06






  • 1




    You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
    – felix
    Nov 16 at 10:21






  • 1




    Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
    – Yksisarvinen
    Nov 16 at 10:26






  • 1




    Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
    – tunglt
    Nov 16 at 10:30








1




1




Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05




Your code is clear and readable to me.
– Jabberwocky
Nov 16 at 10:05




2




2




Object being a class object or not isn't relevant here.
– Jabberwocky
Nov 16 at 10:06




Object being a class object or not isn't relevant here.
– Jabberwocky
Nov 16 at 10:06




1




1




You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21




You can reduce branches by combining them, but this largely dependents on the actual code inside each block. And giving each nullptr test a name (makes them functions) is always a viable solution.
– felix
Nov 16 at 10:21




1




1




Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26




Do you have to accept nulls in your function? Maybe it would be better to forbid passing nullptr to this function (and throw something if that happens).
– Yksisarvinen
Nov 16 at 10:26




1




1




Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30




Your code is clear with detail if condition, it keeps the code easier to maintain and to debug. Put the most passed test in the first if condition to get better performance.
– tunglt
Nov 16 at 10:30












2 Answers
2






active

oldest

votes


















1














You need to perform at most two comparisons. This is how I would do it:



if( obj1 )
{
if( obj2 )
{
// obj1 and obj2 are both valid.
}
else
{
// obj1 is valid, obj2 is nullptr.
}
}
else
{
if( obj2 )
{
// obj1 is nullptr, obj2 is valid.
}
else
{
// Both are nullptr.
}
}





share|improve this answer





























    -2














    The main idea is to reduce the number of if...else branches.
    Maybe, some kind of dispatcher, something like that:



    int ptr_pair_state(const obj1 *p1, const obj1 *p2)
    {
    return (p1 ? 1 : 0) | (p2 ? 2 : 0);
    }

    int Function(Object* obj1, Object* obj2)
    {
    std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
    int state = ptr_pair_state(obj1, obj2);
    disp[state]();
    }


    where f1 - f4 are some functions, for example



    void f1()
    {
    std::cout << "f1";
    }


    However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:



    int Function(Object* obj1, Object* obj2)
    {
    int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
    switch(v)
    {
    case 0: // both nullptr
    break;
    case 1: // p1 not nullptr
    break;
    case 2: // p2 not nullptr
    break;
    case 3: // both not nullptr
    break;
    }
    }


    which much more elegant then endless if...else if






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335450%2fbest-way-to-check-the-null-for-combination-of-two-object%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You need to perform at most two comparisons. This is how I would do it:



      if( obj1 )
      {
      if( obj2 )
      {
      // obj1 and obj2 are both valid.
      }
      else
      {
      // obj1 is valid, obj2 is nullptr.
      }
      }
      else
      {
      if( obj2 )
      {
      // obj1 is nullptr, obj2 is valid.
      }
      else
      {
      // Both are nullptr.
      }
      }





      share|improve this answer


























        1














        You need to perform at most two comparisons. This is how I would do it:



        if( obj1 )
        {
        if( obj2 )
        {
        // obj1 and obj2 are both valid.
        }
        else
        {
        // obj1 is valid, obj2 is nullptr.
        }
        }
        else
        {
        if( obj2 )
        {
        // obj1 is nullptr, obj2 is valid.
        }
        else
        {
        // Both are nullptr.
        }
        }





        share|improve this answer
























          1












          1








          1






          You need to perform at most two comparisons. This is how I would do it:



          if( obj1 )
          {
          if( obj2 )
          {
          // obj1 and obj2 are both valid.
          }
          else
          {
          // obj1 is valid, obj2 is nullptr.
          }
          }
          else
          {
          if( obj2 )
          {
          // obj1 is nullptr, obj2 is valid.
          }
          else
          {
          // Both are nullptr.
          }
          }





          share|improve this answer












          You need to perform at most two comparisons. This is how I would do it:



          if( obj1 )
          {
          if( obj2 )
          {
          // obj1 and obj2 are both valid.
          }
          else
          {
          // obj1 is valid, obj2 is nullptr.
          }
          }
          else
          {
          if( obj2 )
          {
          // obj1 is nullptr, obj2 is valid.
          }
          else
          {
          // Both are nullptr.
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 at 11:38









          James

          492




          492

























              -2














              The main idea is to reduce the number of if...else branches.
              Maybe, some kind of dispatcher, something like that:



              int ptr_pair_state(const obj1 *p1, const obj1 *p2)
              {
              return (p1 ? 1 : 0) | (p2 ? 2 : 0);
              }

              int Function(Object* obj1, Object* obj2)
              {
              std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
              int state = ptr_pair_state(obj1, obj2);
              disp[state]();
              }


              where f1 - f4 are some functions, for example



              void f1()
              {
              std::cout << "f1";
              }


              However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:



              int Function(Object* obj1, Object* obj2)
              {
              int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
              switch(v)
              {
              case 0: // both nullptr
              break;
              case 1: // p1 not nullptr
              break;
              case 2: // p2 not nullptr
              break;
              case 3: // both not nullptr
              break;
              }
              }


              which much more elegant then endless if...else if






              share|improve this answer




























                -2














                The main idea is to reduce the number of if...else branches.
                Maybe, some kind of dispatcher, something like that:



                int ptr_pair_state(const obj1 *p1, const obj1 *p2)
                {
                return (p1 ? 1 : 0) | (p2 ? 2 : 0);
                }

                int Function(Object* obj1, Object* obj2)
                {
                std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
                int state = ptr_pair_state(obj1, obj2);
                disp[state]();
                }


                where f1 - f4 are some functions, for example



                void f1()
                {
                std::cout << "f1";
                }


                However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:



                int Function(Object* obj1, Object* obj2)
                {
                int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
                switch(v)
                {
                case 0: // both nullptr
                break;
                case 1: // p1 not nullptr
                break;
                case 2: // p2 not nullptr
                break;
                case 3: // both not nullptr
                break;
                }
                }


                which much more elegant then endless if...else if






                share|improve this answer


























                  -2












                  -2








                  -2






                  The main idea is to reduce the number of if...else branches.
                  Maybe, some kind of dispatcher, something like that:



                  int ptr_pair_state(const obj1 *p1, const obj1 *p2)
                  {
                  return (p1 ? 1 : 0) | (p2 ? 2 : 0);
                  }

                  int Function(Object* obj1, Object* obj2)
                  {
                  std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
                  int state = ptr_pair_state(obj1, obj2);
                  disp[state]();
                  }


                  where f1 - f4 are some functions, for example



                  void f1()
                  {
                  std::cout << "f1";
                  }


                  However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:



                  int Function(Object* obj1, Object* obj2)
                  {
                  int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
                  switch(v)
                  {
                  case 0: // both nullptr
                  break;
                  case 1: // p1 not nullptr
                  break;
                  case 2: // p2 not nullptr
                  break;
                  case 3: // both not nullptr
                  break;
                  }
                  }


                  which much more elegant then endless if...else if






                  share|improve this answer














                  The main idea is to reduce the number of if...else branches.
                  Maybe, some kind of dispatcher, something like that:



                  int ptr_pair_state(const obj1 *p1, const obj1 *p2)
                  {
                  return (p1 ? 1 : 0) | (p2 ? 2 : 0);
                  }

                  int Function(Object* obj1, Object* obj2)
                  {
                  std::map<int, std::function<void()>> disp{ { 0, f1 }, { 1, f2 }, { 2, f3 }, {3, f4 } };
                  int state = ptr_pair_state(obj1, obj2);
                  disp[state]();
                  }


                  where f1 - f4 are some functions, for example



                  void f1()
                  {
                  std::cout << "f1";
                  }


                  However, this may seem like an unnecessary complication, so another simple alternative - to use switch operator:



                  int Function(Object* obj1, Object* obj2)
                  {
                  int v = (p1 ? 1 : 0) | (p2 ? 2 : 0);
                  switch(v)
                  {
                  case 0: // both nullptr
                  break;
                  case 1: // p1 not nullptr
                  break;
                  case 2: // p2 not nullptr
                  break;
                  case 3: // both not nullptr
                  break;
                  }
                  }


                  which much more elegant then endless if...else if







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 at 11:15

























                  answered Nov 16 at 10:21









                  snake_style

                  1,171310




                  1,171310






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335450%2fbest-way-to-check-the-null-for-combination-of-two-object%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?