By Jasone Tee
TheServerSide.com
Stop doing this:
public boolean foo() {
if (true) {
return true;
} else {
return false;
}
}
It always amazes me when I dig into an open source project, and I see code written by supposed experts, and reviewed by seasoned professionals, and nobody slaps the wrists of the developer who shoves return statements right in the middle of a method.
Tell me, how hard is it to do this:
public boolean foo() {
boolean flag = true;
if (true) {
flag=true;
}
else {
flag=false;
}
return flag;
}
This is Java 101. Actually, it's not even Java 101. It's grade school Java. If a method returns a value, you declare that value as a variable at the start of the method. Then you do things that will give that variable the appropriate meaning. And then, on your last line of code, you return that variable to the calling program. And doing so is more than just about writing good code. It's about being polite.
Have you ever tried to troubleshoot some code that has return statements thrown around all over the method? It's impossible. In fact, the first step in troubleshooting such a method is to rework the darn thing so that it doesn't have a bunch of return statements in it. And it can always be done. There is no method that can't be re-written to have just one, single, easy to identify return statement at the end of it.
Sure, bad programmers have a million different reasons as to why they must program poorly. "I just wanted to avoid a bunch of superfluous conditional statements that the code might have to go through later on." Well, first of all, computers are darned good at quickly evaluating a few conditional statements, so short-circuiting a method to avoid a clock cycle or two is simply ridiculous. And besides, if there's a bunch of superfluous conditional statements that end up getting bypassed, maybe that's a good indication that your 'superfluous' code should be rewritten, perhaps factored out into a different method that isn't superfluous?
The bottom line is that there's no excuse for writing bad code or being a lazy programmer, especially when writing good code isn't that much more difficult. Stop writing methods with a million return statements in them. A method in Java can only return one thing, and correspondingly, a method should have only one return statement in it.
02 Mar 2011
译文
别再这样写了:
public boolean foo() {
if (true) {
return true;
} else {
return false;
}
}
每次当我深入某个开源项目,看到大概是某个专家写的、并被有经验的专业人士审查过的这样的代码,我都会惊讶不已,竟然没有人去阻止这个开发者在这个方法里胡乱的放置返回语句。
请告诉我,把代码写成下面的样子很难吗?
public boolean foo() {
boolean flag = true;
if (true) {
flag=true;
}
else {
flag=false;
}
return flag;
}
这是Java基本常识。实际上,这不仅是Java基本常识,这是小学水平的Java知识。如果你的方法返回一个值,你应该在方法的开始处把它声明做 一个变量。然后再去做一些赋予这个变量正确意义的操作。然后,在你的最后一行,把这个变量返回给调用程序。这样做不仅仅是为写出好的代码,这是一种有教养 的表现。
你是否曾试图修改过一些在方法里到处都是返回语句的程序代码?无从下手。事实上,去维护这样的代码,你第一要做的是重新组织它的结构,让它里面不再有一大 堆的返回语句。这样才能把事情做好。没有任何一个方法是不可以写成只在末尾处有一个的、单一的、易于找到的返回语句的形式的。
的确,烂程序员总有一万个理由来说明他们为什么编写出这样糟糕的程序代码。“我只是为了避免在返回时一堆的多余的条件判断语句。”那好,首先,我告诉你, 计算机中执行一些条件判断语句时是该死的快,你用短路一个方法来节省CPU的一两个指令操作不是显的太荒诞了吗。此外,如果这些所谓多余的条件判断语句最 终没有派上用场的话,这是否是一个有用的信号来说明你的“多余”的代码可能需要重写,也许可以把它们重构成另外一个方法,让它们显的不多余?
关键要说的是,没有任何理由可以为写糟糕的代码或当懒惰的程序员做托辞,特别是当写出好的代码并不是那么困难的情况下。不要在写出里面有成百上千个返回语句的方法了。Java里的方法只可以返回一个值,相应的,一个方法应该有且只有一个返回语句。