Link to home
Start Free TrialLog in
Avatar of yjchong514
yjchong514Flag for United States of America

asked on

Explaination to shorten code

Dear EE members,

What is the explaination to shorten the below code from:  
  return ((aSmile && bSmile) || (!aSmile && !bSmile));

to:
     
  return (aSmile == bSmile);

Regards,

yjchong514
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


 return ((aSmile && bSmile) || (!aSmile && !bSmile));

this will be true if any of the two parts is true --> it will be truye
if either both aSmile and bSmile are true (aSmile && bSmile)
or if they are both false, then (!aSmile && !bSmile));

as boolean can be either true or false this two cases cover
all cases when these two bollean will be equal to each other,
therefore it is enough to check aSmile == bSmile


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And finally you can just check all four cases:

aSmil = true,  bSmile = true ---> ((aSmile && bSmile) || (!aSmile && !bSmile)) true, as aSmile && bSmile this is true
                                                    aSmile == bSmile  also true

aSmile=true  bSmile = false --> ((aSmile && bSmile) || (!aSmile && !bSmile)) false as both parts are false
                                                aSmile == bSmile  also false

aSmaile = ffalse bSmaile = true --->  the same as previous  - false
                                                               aSmile == bSmile  

aSmaile = fdalse bSmile = false --> ((aSmile && bSmile) || (!aSmile && !bSmile))  true as second part is true
                                                           aSmile == bSmile  true
   
So we see that two expressions in all cases give the same value

small correction:
And finally you can just check all four cases:

aSmil = true,  bSmile = true ---> ((aSmile && bSmile) || (!aSmile && !bSmile)) true, as aSmile && bSmile this is true
                                                    aSmile == bSmile  also true

aSmile=true  bSmile = false --> ((aSmile && bSmile) || (!aSmile && !bSmile)) false as both parts are false
                                                aSmile == bSmile  also false

aSmaile = ffalse bSmaile = true --->  the same as previous  - false
                                                               aSmile == bSmile  - false

aSmaile = fdalse bSmile = false --> ((aSmile && bSmile) || (!aSmile && !bSmile))  true as second part is true
                                                           aSmile == bSmile  true
   
So we see that two expressions in all cases give the same value
>>What is the explaination to shorten the below code from:

the second one is the optimized one in such a way that the check happens just once but there is a chance that first one execute two checks. another advantage is the second one is short and clean.
Avatar of yjchong514

ASKER

Thanks all.