Recently I saw pieces of code that
could be simplified. I see such code often, however, recently I came
across code that seemed to follow the same pattern
The patern was as in the following example:
private boolean someFlag;
...
public boolean isSomeFlagSet() {
if (someFlag) {
return true;
}
else {
return false;
}
}
Why not simply write:
public boolean isSomeFlagSet() {
return someFlag;
}
It is so much simpler!
Similarly
to boolean flags, there where occurencies of other object types being
used and methods invoked on them in the following manner:
public String getCity() {
if (address == null) {
return null;
}
else {
return address.getCity();
}
}
This can be also simplified (applying "change if-else to ?:" refactoring) to:
public String getCity() {
return address == null ? null : address.getCity();
}
Please strive for simplicity in your code (K.I.S.S. principle). Verbosity clutters the code and can hide the meaning or intention of it.
No comments:
Post a Comment