Top 10 rules and regulations for Java developers

Source: Internet
Author: User

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 introduced ejbs for applications with only five users. We use a framework for an application, which is not required 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.

 

 


      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, but 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:

 

 


      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.

 

4. Please do not have hard code

Developers often consciously forget or ignore this rule because we are in a hurry, just like in general. If we follow this rule, we may not be able to keep up with the progress. We may not end our current status. But how long does it take us to write an additional code line that defines static constants?

Here is an example.

Public Class {

 

Public static final string s_constant_abc = "ABC ";

 

Public Boolean methoda (string sparam1 ){

If (a.s _ constant_abc.equalsignorecase (sparam1 )){

Return true;

}

Return false;

}

}

Now, every time we compare the string "ABC" with some variables, we only need to reference s_constant_abc, rather than remembering what the actual code is. It also has the advantage that it is easier to modify constants in one place, rather than searching for the code in all the code.

 

 

Do not invent your own frameworks

 

Thousands of frameworks have been launched, and most of them are open-source. Many of these frameworks are excellent solutions and are applied to thousands of applications. You need to keep up with these new frameworks, at least superficial. Among these excellent and widely used frameworks, Struts is the best and most direct example. In Frameworks you can imagine, this open-source Web frameworks is a perfect candidate for Web-based applications. But you must remember the second rule-do not complicate things. If the application you develop has only three pages-please, do not use struts. For such an application, there is no "control" request.

 

6. Do not Add rows and strings.

I know that for debugging purposes, developers like to add the system in every place we think is appropriate. out. println, and we will say to ourselves that we will delete the code later. But we often forget to delete these lines of code, or we don't want to delete them at all. We use system. Out. println for testing. After the test is completed, why can we still access them? We may delete a line of code that we actually need, just because you underestimate the damage caused by system. Out. println, consider the following code:

 

 


      public class BadCode {public static void calculationWithPrint(){double someValue = 0D;for (int i = 0; i < 10000; i++) {System.out.println(someValue = someValue + i);} }public static void calculationWithOutPrint(){double someValue = 0D;for (int i = 0; i < 10000; i++) {someValue = someValue + i;}}public static void main(String [] n) {BadCode.calculationWithPrint();BadCode.calculationWithOutPrint();}}

According to the test, it took 0.001204 seconds to run the calculationwithoutprint () method. In comparison, running the calculationwithprint () method took a surprising 10.52 seconds.

 

(If you don't know how to get a table like this, please refer to my article "Java profiling with wsad" Java profiling with wsad)

The best way to avoid such a CPU waste is to introduce a wrapper method, as shown below:

 

 


      public class BadCode {public static final int DEBUG_MODE = 1;public static final int PRODUCTION_MODE = 2;public static void calculationWithPrint(int logMode){ double someValue = 0D;for (int i = 0; i < 10000; i++) {someValue = someValue + i;myPrintMethod(logMode, someValue);}}public static void myPrintMethod(int logMode, double value) {if (logMode > BadCode.DEBUG_MODE) { return; }System.out.println(value); }public static void main(String [] n) {BadCode.calculationWithPrint(BadCode.PRODUCTION_MODE);}}

According to the test, it took only 0.01 seconds to execute the stringbuffer method, but it took 0.08 seconds to run the stringbuffer method. The selection is obvious.

 

7. Focus on the GUI

No matter how ridiculous it sounds, I need to explain it again: GUI is equally important to commercial customers as it is in terms of functionality and performance. GUI is a necessary part of a successful system. (However) It magazines often tend to ignore the importance of GUI. Many organizations do not hire designers with experience in designing user-friendly guis to save money. Java developers have to rely on their own HTML knowledge, but their knowledge in this area is very limited. I have seen many such applications: they are "computer-friendly", rather than "user-friendly". I rarely see developers who are both proficient in software development and GUI development. If you are the unfortunate developer assigned to develop user interfaces, you should follow the following three principles:

1. Do not reinvent the wheel. Find existing systems with similar user interface requirements.

2. Create a prototype. This is an important step. Customers like to see what they will get. This is also good for you, because you get feedback before you make a user interface that will make users angry.

3. Wear a user's hat. In other words, check the application requirements from the user's perspective. For example, whether a summary page needs to be paged. As a software developer, you tend to ignore pages in a system, because this makes development less complex. However, this is not the best solution from the perspective of a user, because the summary data will have hundreds of data rows.

 

8. Always prepare documented requirements

Every business requirement must be documented. This may come true in some fairy tales, but it is impossible in the real world. No matter how urgent the time is for your development, or whether the delivery date is approaching, you must always be aware that every business requirement is documented.

 

9. Unit Testing, unit testing, and unit testing

I will not discuss in depth the details about what is the best way to perform unit tests on your code. I will talk about the necessity of unit testing. This is the most basic principle of programming. This is the least negligible of all the above rules. This is the best thing if your colleagues can create and test unit tests for your code. But if no one does these tasks for you, you have to do it yourself. Follow these rules when creating your unit test plan:

1. Write unit test cases before writing code.

2. write comments in unit test.

3. test all public methods that execute the "interesting" function ("interesting" means non-setters or getters methods unless they execute the set and get methods in a special way ).

 

10. Remember-quality, not quantity.

Don't stay too late in the Office (when you don't have to stay too late ). I understand that sometimes product problems, urgent deadlines, and unexpected events will prevent us from getting off work on time. However, under normal circumstances, the Manager will not appreciate and reward those employees who are late after work. He appreciates them because of the quality of their products. If you follow the rules above, you will find that your code has fewer bugs and more maintainability. This is the most important part of your work.

 

Summary

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.

 

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.