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
Java

Last Comment
yjchong514
for_yan

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
for_yan

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
arundhaj

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
for_yan

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
for_yan

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
ksivananth

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

Thanks all.
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY