tags:

views:

116

answers:

4

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 ( ($hostNameInfo == $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

+6  A: 

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

if($hostNameInfo != $hostNameInput || $hostAddressInfo != $hostAddressInput) {
   // do something
}
Serty Oan
+1 Having code "do nothing" is not nice practice and should be avoided.
Pekka
IMHO it's cleaner to check preconditions at the beginning of the function and return if there isn't need to do anything. And you save one indentation level! :)
phadej
@phadej : I personnaly find it cleaner to have only one `return` in a function and not use it as a void return. And in the case it is a precodition, why not checking before calling the function ?
Serty Oan
@Serty: Sorry, I've edited my code, it's not just a if condition is a if else. Still, does your answer still applies? I mean, the best way do deal with this "do nothing" is to, actually ignore it? Am I getting this?
MEM
@Serty: It depends. I mean by preconditions the checks as in the question. They may be so complicated or just that long,that it's easier to break them apart than trying to add up them into one conditional. Also you may argue to follow jensgram solution, but it's not always cheap to calculate all of them, sometimes even impossible, as if later checks are not sensible if earlier are already failed.
phadej
@MEM : "the best way do deal with this "do nothing" is to, actually ignore it?", indeed. @phadej : I think all of this is pretty subjective (like all coding conventions), in case of many checks with no else, I would use only one if (with some indentations in the if condition to make it readable), everyone has his own habits and likes :)
Serty Oan
A: 

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.

Viktor Stískala
+1  A: 

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).

jensgram
Ohh... so we can not only store values on variables but, we can store, as well, states or conditions? I didn't know about that.So, and according to Serty Oan answer and Pekka comments, we should know that if we arrive on a situation where we have to comment like //do nothing that probably means that we are NOT properly structuring our code hm?
MEM
I will consider Serty Oan answer but I will mark yours as relevant. Thanks a lot for your time, I will ask my comment question on another question. It's a self question by itself. :)Thanks a lot again,MEM
MEM
@MEM Well, the stored "condition" (e.g., `$fieldsAreFilled`) is really just a Boolean value of the expression on the right-hand side. No magic here.
jensgram
Well... but I'm having troubles to understand the conditional later on: if(true and !true) do something? What does that mean?... Reply if you feel like, of course.. kind of lost.
MEM
jensgram
I was missing the context of the true and false. Clear now. ;) Thanks.
MEM
BTW You might find this interesting: http://en.wikipedia.org/wiki/Return_statement#Criticism
jensgram
+1  A: 

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';
}
Andrejs Cainikovs
Thanks for that single exit point. I didn't know about it. It's related to functions, but my question misslead to that direction. Thanks for your time on updating your question. I will mark your question as relevant. Regards.
MEM