tags:

views:

142

answers:

6

A = if infos !empty and inputs empty - do remove;

B = if infos empty and inputs !empty - do add;

C = if infos !empty and inputs !equal to infos - do add;

We can have like:

if B //it's the most common operation, so at the beginning.
{
  //add 
}
else
{
 //remove
}
elseif(c) 
{
 //the same add
} 

I believe this can be better thinking. Can I have your help?

Thanks in advance,

A: 

You sure can do that. Just order the conditional blocks: If... Else If... Else.

Daniel A. White
MEM
@mem - No, it will only be executed if none of the other blocks are executed.
Daniel A. White
@Daniel: Thanks - So else, doesn't mean the opposite, or the contrary. Just, something different. ?
MEM
+7  A: 
if (B || C) 
{
  //add 
}
else
{
 //remove
}
Dezigo
The only think here, is that, the C condition plus the B condition could because quite big. :s So, I better store those comparisons into a variable, and use those variables instead. What do you think?
MEM
This would also remove when both are empty.
Gumbo
@Gumbo: Please clarify - both what - infos or inputs ?
MEM
ow... infos and inputs ? if both infos and inputs are empty this will not properly work?
MEM
@MEM if you want to use the B and C conditions later on, store them in a variable, if you only want to use them once, put them directly in the condition. be sure to use (B) || (C), so nested conditions, if any, doesn't get evaluated in a way you don't want.
aularon
@MEM: Yes, I meant *infos* and *inputs*. But I have to admit that I was wrong with my objection (overlooked the “!” in “!equal”). But this will fail if `!empty($infos)` but `$inputs == $infos`. Because in that case this will run the *remove* branch although the second condition `empty($inputs)` is not fulfilled.
Gumbo
Sorry Gumbo: (kidda slow today) - when you mention "this" do you mean the Dezigo answer, our your objection? Thanks.
MEM
@MEM: “This” refers to Dezigo’s answer.
Gumbo
@All - So, the else branch (remove) will execute when B or C are not the case.If !empty infos and input==infos, then the else will run. Hence, the remove will be executed. However, the intend result is to ignore, and do nothing if that's the case, SO, this answers as Gumbo noted, seems not to be appropriate. Yes?
MEM
+2  A: 

it's if, elseif (as much elseif's as you want) and finally else:

if B //it's the most common operation, so at the beginning.
{
  //add 
} elseif(something else) 
{
 //the same add
} elseif(c) 
{
 //the same add
} else
{
 //remove
}
aularon
The thing is, that we are repeating the add code, it's exactly the same, so probably having only one "add" will be better?
MEM
So put the add code in a different method...
Roger Lipscombe
I see... we are repeating the code, so either we do like Dezigo suggest, or we grab that repetition and convert that into a method? Or I'm not properly understand it ?
MEM
You are right, I didn't notice he wants to add in both cases.
aularon
I really like your if elseif else representation. :) I will mark as useful. :)
MEM
+1  A: 
if( !A ) {
  add;
} else {
  remove;
}

Short and clear in my opinion.

InsertNickHere
But wrong when both are empty.
Gumbo
@Gumbo you are correct. Dident see this case in the specs so i missed it.
InsertNickHere
A: 

You can combine the conditions of B and C with the OR operator:

if (empty($infos) && !empty($inputs) || !empty($infos) && $inputs != $infos) {
    // add
} else if (!empty($infos) && empty($inputs)) {
    // remove
}
Gumbo
Can you please comment on your comments, when you refer to both, what are you referring to? :D
MEM
got it. :) info and inputs
MEM
What’s the reason for the down vote?
Gumbo
I'm with Gumbo. :) I believe this info is helpfull.
MEM
+1  A: 
if (infos != inputs) {
    if (empty(inputs)) {
        // remove
    } else {
        // add
    }
}

Remember, the outermost condition checks that both values are never empty (never the same, actually). E.g.,

A = if infos !empty and inputs empty - do remove;

If inputs is empty infos can not be empty. Therefore, remove.

B = if infos empty and inputs !empty - do add;
C = if infos !empty and inputs !equal to infos - do add;

Different and inputs not empty => it doesn't mather whether info is empty => add.

jensgram
I will study it...
MEM
I believe this covers all the possibilities. If they are equal, do nothing.If they are different, and input is empty - do remove.If they are different, and input isn't empty - do add.
MEM