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
Java

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

Blurred text
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.
See Pricing Options
Start Free Trial
Avatar of for_yan
for_yan
Flag of United States of America image



 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
Avatar of arundhaj
arundhaj
Flag of India image

Blurred text
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.
Avatar of for_yan
for_yan
Flag of United States of America image

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
Avatar of for_yan
for_yan
Flag of United States of America image


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
Avatar of ksivananth
ksivananth
Flag of United States of America image

>>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
yjchong514
Flag of United States of America image

ASKER

Thanks all.
Java
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
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo