//first
if(num % 2 == 0 ) {
isEven = true;
}
//second
isEven = (num %2 == 0);
What is the best thing to do, and is first case a case of code smell?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They don’t do the same thing – if
numis odd, the first leavesisEvenwith its previous value, the second sets it tofalse.I would:
When the body of an
ifblock is just setting a variable, and the value can be expressed as some simple modification of the condition of theifblock, and you always want to set some value, I would just use the simple assignment.The same goes for
returnstatements – I would rather have:than