I don't agree with "single exit point" and so did I practically never apply it.
Glad to find an entry on this website:
http://stackoverflow.com/questions/1701686/why-should-methods-have-a-single-entry-and-exit-points
It said that:
This advice is outdated and bad for code pratice because it will lead to temporary variable which is hard to maintain.
To me, temporary variables smell bad a lot rather that multiple exit points.
public void uglyAndWrong(final int hamsandwich) {
int answer;
if (hamsandwich % 2 == 0) {
answer = 27;
} else {
answer = 13;
}
return answer;
}
vs:
public void comelyAndHip(final int hamsandwich) {
if (hamsandwich % 2 == 0) {
return 27;
}
return 13;
}
How about multiple entry point ?
Look at this code:
public void doSomething(MyObject person, boolean beNice) {
if (beNice) {
//code to do something nice
} else {
//code to do something not nice
}
}
vs:
public void doSomethingNice(MyObject person) {
//code to do something nice
}
public void doSomethingNotNice(MyObject person) {
//code to do something not nice
}
Which one do you prefer ?
I prefer the first one coz it would reduce code duplications because most of the time, code to "do something nice" will just be different in a little way with "do something not nice".
0 comments: (+add yours?)
Post a Comment