Top 10 basic rules that Java developers must follow

Source: Internet
Author: User
In this article, I have given ten important rules for Java developers. It is important not only to know these rules, but to follow these rules during coding. We hope these rules will help us become better programmers and professionals.

Java developers have many standards and best practices. This article lists the top ten basic rules that every developer must follow. if there are rules that can be followed but not followed, it will lead to a very miserable ending.

1. add comments to your code

Everyone knows this, but somehow forgets to follow it. How many times have you "forgot" to add comments? This is the fact that annotations do not actually contribute to the functionality of the program. However, you need to go back to the code you wrote two weeks ago. it may be the same for the rest of your life. you cannot remember why the code is like this. If the code is yours, you are lucky. Because it may make you recall. But unfortunately, for a lot of time, this code is owned by others, and it is very likely that he has left the company.

2. don't complicate things

I have done this before, and I believe everyone has done this. Developers often propose a solution for a simple problem. We introduce EJBs for applications with only five users. We use the framework for an application and it does not need it at all. We add attribute files, object-oriented solutions, and threads to the application, but it does not need these. Why do we do this? Some of us do not know how to do better, but some others aim to learn new knowledge and make this application interesting for ourselves.

3. keep in mind that "less is more" is not always good.

Code efficiency is a great thing, but in many cases, writing less code lines does not improve the efficiency of the code. Let me show you a simple example.

Reference content is as follows:
If (newStatusCode. equals ("SD") & (sellOffDate = null |

TodayDate. compareTo (sellOffDate) <0 | (lastUsedDate! = Null &&

TodayDate. compareTo (lastUsedDate)> 0) |

(NewStatusCode. equals ("OBS") & (OBSDate = null |

TodayDate. compareTo (OBSDate) <0 ))){

NewStatusCode = "NYP ";

}

I would like to ask: is it easy to say what the if condition of the above code is? Now, let's assume that no matter who writes this code, it does not follow the first rule ?? Add comments to your code.

If we divide this condition into two independent if statements, isn't it simpler? Now, consider the following correction code:

 

Reference content is as follows:
If (newStatusCode. equals ("SD") & (sellOffDate = null |

TodayDate. compareTo (sellOffDate) <0 | (lastUsedDate! = Null &&

TodayDate. compareTo (lastUsedDate)> 0 ))){

NewStatusCode = "NYP ";

} Else

If (newStatusCode. equals ("OBS") & (OBSDate = null |

TodayDate. compareTo (OBSDate) <0 ))

{

NewStatusCode = "NYP ";

}

 

Isn't it more readable? Yes, we repeat the statement conditions. Yes, we have an extra "IF" and two pairs of redundant arc. But the code has better readability and comprehensibility.

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.