0
Smell code identification
b) Consider the following java code (method) compares 2 numbers and returns true if the first is greater and false otherwise. Do you think there is bad smells in this code, if yes than identify its type, and write a new code by using refactoring approach to remove the code smell. public boolean max(int a, int b) { if(a > b) { return true; } else if (a == b) { return false; } else { return false; } }
3 Réponses
+ 7
1. name of function is not compatible for it's functionality.
2. return a > b; this one line can do what whole your code do.
+ 2
else-if code block is unnecessary.
This is a third thread of same question.
Other two being
https://www.sololearn.com/Discuss/2599334/?ref=app
and
https://www.sololearn.com/discuss/2599349/?ref=app
Do NOT repeatingly post the same question!
+ 2
public boolean max(int a, int b)
{
return (a > b);
}
The "smell" came from unnecessary `else...if` and `else` block. The function is supposed to return 'true' ONLY when <a> is greater than <b>, so we can do this simply by returning the evaluation result for expression (a > b).