Good code as long as a return statement

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags code developer dig it is java network network programming no one

Do not write this again:
public boolean foo () {
if (true) {
return true;
} else {
return false;
}
}
Every time I dig into an open source project and see something like that written by an expert and reviewed by an experienced professional, I'm surprised that no one has stopped the developer from doing anything in this way Randomly placed return statement.
Please tell me, the code written in the following look hard?
public boolean foo () {
boolean flag = true;
if (true) {
flag = true;
}
else {
flag = false;
}
return flag;
}
This is a basic Java knowledge. In fact, this is not only Java basic knowledge, which is elementary level Java knowledge. If your method returns a value, you should declare it as a variable at the beginning of the method. Then do something that gives the correct meaning to this variable. Then, on your last line, return this variable to the caller. This is not just a matter of educating you to write good code.
Have you ever tried to modify some of the code in the method where the return statement is everywhere? In fact, the first thing you need to do to maintain this code is to reorganize its structure so that there is not a whole lot of return statements in it. Only in this way can we get things done. Either way can not be written in the form of a single, easy-to-find return statement that has only one at the end.
Indeed, rotten programmers have ten thousand reasons to explain why they wrote such a bad code. "I'm just trying to avoid throwing back a bunch of extra conditional statements." Well, first of all, I'm telling you that executing some conditional statements in a computer is damn fast, and you short-circuit a way to save the CPU Two instruction operation is not obvious too absurd it. In addition, if these so-called redundant conditional statements eventually come in handy, is this a useful signal to show that your "extra" code may need to be overridden, perhaps by refactoring them into another way to get them Obviously not redundant?
The key thing to say is that there is no reason to make excuses for writing bad code or as a lazy programmer, especially when writing good code is not that hard. Do not write out there are hundreds of ways to return statements. A method in Java can return only one value, and accordingly, a method should have one and only one return statement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.