PHP function to evaluate comparisons between two values, with debug logging
Clash 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.
php comparative-review
migrated from stackoverflow.com Jul 10 at 12:09
This question came from our site for professional and enthusiast programmers.
add a comment |Â
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.
php comparative-review
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 likeperformComparison($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 alogComparison
function that you call after making a comparison notperformComparsion
where it just has those 3 lines after the switch. I know if I came across a code base that made use of aperformComparison
function I would forget to use it in favor of making the actual comparison.
â Shelby115
Jul 10 at 12:26
add a comment |Â
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.
php comparative-review
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.
php comparative-review
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 likeperformComparison($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 alogComparison
function that you call after making a comparison notperformComparsion
where it just has those 3 lines after the switch. I know if I came across a code base that made use of aperformComparison
function I would forget to use it in favor of making the actual comparison.
â Shelby115
Jul 10 at 12:26
add a comment |Â
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 likeperformComparison($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 alogComparison
function that you call after making a comparison notperformComparsion
where it just has those 3 lines after the switch. I know if I came across a code base that made use of aperformComparison
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Jul 11 at 3:19
answered Jul 10 at 12:32
Your Common Sense
2,405523
2,405523
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 alogComparison
function that you call after making a comparison notperformComparsion
where it just has those 3 lines after the switch. I know if I came across a code base that made use of aperformComparison
function I would forget to use it in favor of making the actual comparison.â Shelby115
Jul 10 at 12:26