开发者

PHP - If something is the case, do nothing

Is this a proper way to say: if something is the case, do nothing?

if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
  return;
}

Update: I'm not inside a function. :( So the return is just nonsense.

Here is more code:

//if the input fields are equal to database values, no need to update and waste resources,hence, do nothing:
if ( ($host开发者_JAVA技巧NameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
  //do nothing
}
//If, however, (they are NOT equal and) input fields are not empty:
elseif (!empty($hostNameInput) && (!empty($hostAddressInput)))
{
 //do something.
}

Thanks in advance, MEM


For do nothing you simply can type:

function relax() {
    ;
}

if (($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput)) {
    relax();
}


Maybe you should do the opposite, do something if your condition is not verified

if($hostNameInfo != $hostNameInput || $hostAddressInfo != $hostAddressInput) {
   // do something
}


I assume you're inside a function in which case it does what you expect, although multiple return statements within a function can lead to confusion and a lack of readability. (Apparently I was wrong.)

Instead, I prefer to let all conditional blocks (my description for the code between in the if's {...} block) contain the relevant code, i.e., write the conditional check in such a way that the total condition evaluates to true when additional processing (sub-flow) is needed:

if ($hostNameInfo != $hostNameInput || $hostAddressInfo != $hostAddressInput) {
    // do stuff, else skip
}

Furthermore, you can extract the conditional statement in order to improve both readability and simplicity of control flow:

$hostInfoEqualsInput = ($hostNameInfo == $hostNameInput && $hostAddressInfo == $hostAddressInput);
if (!$hostInfoEqualsInput) {
    ...
}

UPDATE (based on updated question). Consider this instead:

$fieldsAreFilled = (!empty($hostNameInput) && !empty($hostAddressInput));
$hostInfoEqualsInput = ($hostNameInfo == $hostNameInput && $hostAddressInfo == $hostAddressInput);

if ($fieldsAreFilled && !$hostInfoEqualsInput) {
    ...
}

ERGO
Minimize branch rate and avoid empty blocks by writing conditions you want to be met, not all the exceptions you want to ignore (subjective).


You're talking about best practices here..
One of best practice things is that routine shall have single exit point, though it is widely discussed and is up to developer/style.

UPDATE:

New answer, since the question was changed:

Don't see any reason to add additional checks if the code should run only under some circustances. To make the code more readable, you should stuck to whatever you accept as easy-maintainable, like this (or something similar):

// Do something only if required
if (($hostNameInfo != $hostNameInput) || ($hostAddressInfo != $hostAddressInput)) &&
    !empty($hostNameInput) && !empty($hostAddressInput))
{
    echo 'place some code here';
}


A native do_nothing() function would be very nice and readable sometimes.

To avoid stressing alerts from syntax checkers & linters, that go crazy when you have an empty if block, I use:

echo(null);


The other possibility is to throw a new exception, which you can later catch in your application.

UPDATE: not inside the function this is probably a bad idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜