PHP function to evaluate comparisons between two values, with debug logging

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












The following method works fine but it is rather verbose:



private function performComparison($element, $operator, $value_allowed, $value_entered)

switch($operator):
case '>':
$result = ($value_entered > $value_allowed) ? true : false;
break;
case '>=':
$result = ($value_entered >= $value_allowed) ? true : false;
break;
case '<':
$result = ($value_entered < $value_allowed) ? true : false;
break;
case '<=':
$result = ($value_entered <= $value_allowed) ? true : false;
break;
case '=':
$result = ($value_entered == $value_allowed) ? true : false;
break;
endswitch;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



Note that the 3 lines of code just before the return $result; line are very helpful for debugging in the Development phase so I'd like to keep those.



In an attempt to make a more concise version of this method I came up with the following:



private function performComparison($element, $operator, $value_allowed, $value_entered)

$comparisonFunctions = [
'>' => function($value_allowed, $value_entered) return ($value_entered > $value_allowed) ? true : false; ,
'>=' => function($value_allowed, $value_entered) return ($value_entered >= $value_allowed) ? true : false; ,
'<' => function($value_allowed, $value_entered) return ($value_entered < $value_allowed) ? true : false; ,
'<=' => function($value_allowed, $value_entered) return ($value_entered <= $value_allowed) ? true : false; ,
'=' => function($value_allowed, $value_entered) return ($value_entered == $value_allowed) ? true : false;
];

$result = array_key_exists($operator, $comparisonFunctions) ? $comparisonFunctions[$operator]($value_allowed, $value_entered) : null;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



This version also works fine but I'm wondering if there are any ways it could be condensed further.







share|improve this question













migrated from stackoverflow.com Jul 10 at 12:09


This question came from our site for professional and enthusiast programmers.










  • 3




    At the very least, ... ? true : false is always too verbose and can be omitted.
    – deceze
    Jul 10 at 12:10










  • Could you explain what this function is actually for? I'm finding it hard to understand why you'd do something like performComparison($element, $operator, $value_allowed, $value_entered) instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have a logComparison function that you call after making a comparison not performComparsion where it just has those 3 lines after the switch. I know if I came across a code base that made use of a performComparison function I would forget to use it in favor of making the actual comparison.
    – Shelby115
    Jul 10 at 12:26

















up vote
3
down vote

favorite












The following method works fine but it is rather verbose:



private function performComparison($element, $operator, $value_allowed, $value_entered)

switch($operator):
case '>':
$result = ($value_entered > $value_allowed) ? true : false;
break;
case '>=':
$result = ($value_entered >= $value_allowed) ? true : false;
break;
case '<':
$result = ($value_entered < $value_allowed) ? true : false;
break;
case '<=':
$result = ($value_entered <= $value_allowed) ? true : false;
break;
case '=':
$result = ($value_entered == $value_allowed) ? true : false;
break;
endswitch;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



Note that the 3 lines of code just before the return $result; line are very helpful for debugging in the Development phase so I'd like to keep those.



In an attempt to make a more concise version of this method I came up with the following:



private function performComparison($element, $operator, $value_allowed, $value_entered)

$comparisonFunctions = [
'>' => function($value_allowed, $value_entered) return ($value_entered > $value_allowed) ? true : false; ,
'>=' => function($value_allowed, $value_entered) return ($value_entered >= $value_allowed) ? true : false; ,
'<' => function($value_allowed, $value_entered) return ($value_entered < $value_allowed) ? true : false; ,
'<=' => function($value_allowed, $value_entered) return ($value_entered <= $value_allowed) ? true : false; ,
'=' => function($value_allowed, $value_entered) return ($value_entered == $value_allowed) ? true : false;
];

$result = array_key_exists($operator, $comparisonFunctions) ? $comparisonFunctions[$operator]($value_allowed, $value_entered) : null;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



This version also works fine but I'm wondering if there are any ways it could be condensed further.







share|improve this question













migrated from stackoverflow.com Jul 10 at 12:09


This question came from our site for professional and enthusiast programmers.










  • 3




    At the very least, ... ? true : false is always too verbose and can be omitted.
    – deceze
    Jul 10 at 12:10










  • Could you explain what this function is actually for? I'm finding it hard to understand why you'd do something like performComparison($element, $operator, $value_allowed, $value_entered) instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have a logComparison function that you call after making a comparison not performComparsion where it just has those 3 lines after the switch. I know if I came across a code base that made use of a performComparison function I would forget to use it in favor of making the actual comparison.
    – Shelby115
    Jul 10 at 12:26













up vote
3
down vote

favorite









up vote
3
down vote

favorite











The following method works fine but it is rather verbose:



private function performComparison($element, $operator, $value_allowed, $value_entered)

switch($operator):
case '>':
$result = ($value_entered > $value_allowed) ? true : false;
break;
case '>=':
$result = ($value_entered >= $value_allowed) ? true : false;
break;
case '<':
$result = ($value_entered < $value_allowed) ? true : false;
break;
case '<=':
$result = ($value_entered <= $value_allowed) ? true : false;
break;
case '=':
$result = ($value_entered == $value_allowed) ? true : false;
break;
endswitch;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



Note that the 3 lines of code just before the return $result; line are very helpful for debugging in the Development phase so I'd like to keep those.



In an attempt to make a more concise version of this method I came up with the following:



private function performComparison($element, $operator, $value_allowed, $value_entered)

$comparisonFunctions = [
'>' => function($value_allowed, $value_entered) return ($value_entered > $value_allowed) ? true : false; ,
'>=' => function($value_allowed, $value_entered) return ($value_entered >= $value_allowed) ? true : false; ,
'<' => function($value_allowed, $value_entered) return ($value_entered < $value_allowed) ? true : false; ,
'<=' => function($value_allowed, $value_entered) return ($value_entered <= $value_allowed) ? true : false; ,
'=' => function($value_allowed, $value_entered) return ($value_entered == $value_allowed) ? true : false;
];

$result = array_key_exists($operator, $comparisonFunctions) ? $comparisonFunctions[$operator]($value_allowed, $value_entered) : null;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



This version also works fine but I'm wondering if there are any ways it could be condensed further.







share|improve this question













The following method works fine but it is rather verbose:



private function performComparison($element, $operator, $value_allowed, $value_entered)

switch($operator):
case '>':
$result = ($value_entered > $value_allowed) ? true : false;
break;
case '>=':
$result = ($value_entered >= $value_allowed) ? true : false;
break;
case '<':
$result = ($value_entered < $value_allowed) ? true : false;
break;
case '<=':
$result = ($value_entered <= $value_allowed) ? true : false;
break;
case '=':
$result = ($value_entered == $value_allowed) ? true : false;
break;
endswitch;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



Note that the 3 lines of code just before the return $result; line are very helpful for debugging in the Development phase so I'd like to keep those.



In an attempt to make a more concise version of this method I came up with the following:



private function performComparison($element, $operator, $value_allowed, $value_entered)

$comparisonFunctions = [
'>' => function($value_allowed, $value_entered) return ($value_entered > $value_allowed) ? true : false; ,
'>=' => function($value_allowed, $value_entered) return ($value_entered >= $value_allowed) ? true : false; ,
'<' => function($value_allowed, $value_entered) return ($value_entered < $value_allowed) ? true : false; ,
'<=' => function($value_allowed, $value_entered) return ($value_entered <= $value_allowed) ? true : false; ,
'=' => function($value_allowed, $value_entered) return ($value_entered == $value_allowed) ? true : false;
];

$result = array_key_exists($operator, $comparisonFunctions) ? $comparisonFunctions[$operator]($value_allowed, $value_entered) : null;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;



This version also works fine but I'm wondering if there are any ways it could be condensed further.









share|improve this question












share|improve this question




share|improve this question








edited Jul 11 at 4:53









200_success

123k14143399




123k14143399









asked Jul 10 at 12:05









knot22

1182




1182




migrated from stackoverflow.com Jul 10 at 12:09


This question came from our site for professional and enthusiast programmers.






migrated from stackoverflow.com Jul 10 at 12:09


This question came from our site for professional and enthusiast programmers.









  • 3




    At the very least, ... ? true : false is always too verbose and can be omitted.
    – deceze
    Jul 10 at 12:10










  • Could you explain what this function is actually for? I'm finding it hard to understand why you'd do something like performComparison($element, $operator, $value_allowed, $value_entered) instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have a logComparison function that you call after making a comparison not performComparsion where it just has those 3 lines after the switch. I know if I came across a code base that made use of a performComparison function I would forget to use it in favor of making the actual comparison.
    – Shelby115
    Jul 10 at 12:26













  • 3




    At the very least, ... ? true : false is always too verbose and can be omitted.
    – deceze
    Jul 10 at 12:10










  • Could you explain what this function is actually for? I'm finding it hard to understand why you'd do something like performComparison($element, $operator, $value_allowed, $value_entered) instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have a logComparison function that you call after making a comparison not performComparsion where it just has those 3 lines after the switch. I know if I came across a code base that made use of a performComparison function I would forget to use it in favor of making the actual comparison.
    – Shelby115
    Jul 10 at 12:26








3




3




At the very least, ... ? true : false is always too verbose and can be omitted.
– deceze
Jul 10 at 12:10




At the very least, ... ? true : false is always too verbose and can be omitted.
– deceze
Jul 10 at 12:10












Could you explain what this function is actually for? I'm finding it hard to understand why you'd do something like performComparison($element, $operator, $value_allowed, $value_entered) instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have a logComparison function that you call after making a comparison not performComparsion where it just has those 3 lines after the switch. I know if I came across a code base that made use of a performComparison function I would forget to use it in favor of making the actual comparison.
– Shelby115
Jul 10 at 12:26





Could you explain what this function is actually for? I'm finding it hard to understand why you'd do something like performComparison($element, $operator, $value_allowed, $value_entered) instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have a logComparison function that you call after making a comparison not performComparsion where it just has those 3 lines after the switch. I know if I came across a code base that made use of a performComparison function I would forget to use it in favor of making the actual comparison.
– Shelby115
Jul 10 at 12:26











1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










given your function's purpose is to return the calculation result, I would return the expression result right away, especially given return makes break unnecessary as well



private function performComparison($operator, $value_allowed, $value_entered)

switch($operator)
case '>':
return ($value_entered > $value_allowed);
case '>=':
return ($value_entered >= $value_allowed);
case '<':
return ($value_entered < $value_allowed);
case '<=':
return ($value_entered <= $value_allowed);
case '=':
return ($value_entered == $value_allowed);
default:
throw new InvalidArgumentException("Invalid operator");




The code above is both concise and readable, it takes one no problem to gist the function's meaning and purpose.



Whereas any debugging should be done elsewhere. If you need any debugging for this function, create another one encapsulating the current function's call.



 private function debugPerformComparison($element, $operator, $value_allowed, $value_entered)

$result = $this->performComparison($operator, $value_allowed, $value_entered);
$dateformat = 'n/j/Y';
$debug = [
'element' => $element,
'operator' => $operator,
'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed,
'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered,
'result' => $result,
];
echo '<pre>'.var_export($debug,1).'</pre>';
return $result;



Regarding anonymous function-based version, a concise version shouldn't be less readable in the first place. Any code improvements should improve the readability, by means of removing unnecessary parts, not worsen it. And the said version is extremely hard to read.






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    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: "196"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198216%2fphp-function-to-evaluate-comparisons-between-two-values-with-debug-logging%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    given your function's purpose is to return the calculation result, I would return the expression result right away, especially given return makes break unnecessary as well



    private function performComparison($operator, $value_allowed, $value_entered)

    switch($operator)
    case '>':
    return ($value_entered > $value_allowed);
    case '>=':
    return ($value_entered >= $value_allowed);
    case '<':
    return ($value_entered < $value_allowed);
    case '<=':
    return ($value_entered <= $value_allowed);
    case '=':
    return ($value_entered == $value_allowed);
    default:
    throw new InvalidArgumentException("Invalid operator");




    The code above is both concise and readable, it takes one no problem to gist the function's meaning and purpose.



    Whereas any debugging should be done elsewhere. If you need any debugging for this function, create another one encapsulating the current function's call.



     private function debugPerformComparison($element, $operator, $value_allowed, $value_entered)

    $result = $this->performComparison($operator, $value_allowed, $value_entered);
    $dateformat = 'n/j/Y';
    $debug = [
    'element' => $element,
    'operator' => $operator,
    'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed,
    'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered,
    'result' => $result,
    ];
    echo '<pre>'.var_export($debug,1).'</pre>';
    return $result;



    Regarding anonymous function-based version, a concise version shouldn't be less readable in the first place. Any code improvements should improve the readability, by means of removing unnecessary parts, not worsen it. And the said version is extremely hard to read.






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      given your function's purpose is to return the calculation result, I would return the expression result right away, especially given return makes break unnecessary as well



      private function performComparison($operator, $value_allowed, $value_entered)

      switch($operator)
      case '>':
      return ($value_entered > $value_allowed);
      case '>=':
      return ($value_entered >= $value_allowed);
      case '<':
      return ($value_entered < $value_allowed);
      case '<=':
      return ($value_entered <= $value_allowed);
      case '=':
      return ($value_entered == $value_allowed);
      default:
      throw new InvalidArgumentException("Invalid operator");




      The code above is both concise and readable, it takes one no problem to gist the function's meaning and purpose.



      Whereas any debugging should be done elsewhere. If you need any debugging for this function, create another one encapsulating the current function's call.



       private function debugPerformComparison($element, $operator, $value_allowed, $value_entered)

      $result = $this->performComparison($operator, $value_allowed, $value_entered);
      $dateformat = 'n/j/Y';
      $debug = [
      'element' => $element,
      'operator' => $operator,
      'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed,
      'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered,
      'result' => $result,
      ];
      echo '<pre>'.var_export($debug,1).'</pre>';
      return $result;



      Regarding anonymous function-based version, a concise version shouldn't be less readable in the first place. Any code improvements should improve the readability, by means of removing unnecessary parts, not worsen it. And the said version is extremely hard to read.






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        given your function's purpose is to return the calculation result, I would return the expression result right away, especially given return makes break unnecessary as well



        private function performComparison($operator, $value_allowed, $value_entered)

        switch($operator)
        case '>':
        return ($value_entered > $value_allowed);
        case '>=':
        return ($value_entered >= $value_allowed);
        case '<':
        return ($value_entered < $value_allowed);
        case '<=':
        return ($value_entered <= $value_allowed);
        case '=':
        return ($value_entered == $value_allowed);
        default:
        throw new InvalidArgumentException("Invalid operator");




        The code above is both concise and readable, it takes one no problem to gist the function's meaning and purpose.



        Whereas any debugging should be done elsewhere. If you need any debugging for this function, create another one encapsulating the current function's call.



         private function debugPerformComparison($element, $operator, $value_allowed, $value_entered)

        $result = $this->performComparison($operator, $value_allowed, $value_entered);
        $dateformat = 'n/j/Y';
        $debug = [
        'element' => $element,
        'operator' => $operator,
        'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed,
        'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered,
        'result' => $result,
        ];
        echo '<pre>'.var_export($debug,1).'</pre>';
        return $result;



        Regarding anonymous function-based version, a concise version shouldn't be less readable in the first place. Any code improvements should improve the readability, by means of removing unnecessary parts, not worsen it. And the said version is extremely hard to read.






        share|improve this answer















        given your function's purpose is to return the calculation result, I would return the expression result right away, especially given return makes break unnecessary as well



        private function performComparison($operator, $value_allowed, $value_entered)

        switch($operator)
        case '>':
        return ($value_entered > $value_allowed);
        case '>=':
        return ($value_entered >= $value_allowed);
        case '<':
        return ($value_entered < $value_allowed);
        case '<=':
        return ($value_entered <= $value_allowed);
        case '=':
        return ($value_entered == $value_allowed);
        default:
        throw new InvalidArgumentException("Invalid operator");




        The code above is both concise and readable, it takes one no problem to gist the function's meaning and purpose.



        Whereas any debugging should be done elsewhere. If you need any debugging for this function, create another one encapsulating the current function's call.



         private function debugPerformComparison($element, $operator, $value_allowed, $value_entered)

        $result = $this->performComparison($operator, $value_allowed, $value_entered);
        $dateformat = 'n/j/Y';
        $debug = [
        'element' => $element,
        'operator' => $operator,
        'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed,
        'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered,
        'result' => $result,
        ];
        echo '<pre>'.var_export($debug,1).'</pre>';
        return $result;



        Regarding anonymous function-based version, a concise version shouldn't be less readable in the first place. Any code improvements should improve the readability, by means of removing unnecessary parts, not worsen it. And the said version is extremely hard to read.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jul 11 at 3:19


























        answered Jul 10 at 12:32









        Your Common Sense

        2,405523




        2,405523






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198216%2fphp-function-to-evaluate-comparisons-between-two-values-with-debug-logging%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?