Top 10 Java programmers

Source: Internet
Author: User
Document directory
  • Conclusion

Java programmers have many rules or best practices to follow. This article provides an overview of the top 10 rules or prohibitions that each developer should follow. If they are not followed, catastrophic consequences will occur.

1. Add comments to your code ).-Everyone knows this, but not everyone does. How many times have you "forgot" to add comments? Indeed, Annotations do not add any function functions to your program. However, how many times can't you remember what the code was written two weeks ago? You are lucky to have written uncommented code on your own, and you will have a lingering impression in your mind. Unfortunately, most of the time, the Code was written by someone else, and that person may have left the company. There is a saying that "there are future and mutual benefit", so programmers should be considerate of each other (and yourself) and add comments to your code.

2. Do not complicate simple things (do not complicate things ).-I have done this before, and I believe you are the same. Developers tend to solve simple problems in a complex way. We introduce ejbs into a system with only five users and implement a framework for an application that does not need a framework. This framework uses attribute files, object-oriented solutions, and threads, these are not needed at all. Why? Some may not know a better solution, but others may deliberately learn new knowledge, or just for fun. For those who do not know a better solution, they should listen to the suggestions of experienced programmers. For those who complicate the design purely for personal purposes, I suggest you be more professional.

3. Remember-"The less the better" is not always the case (keep in mind-"less is more" is not always better ).-Efficient code is a good thing, but in many cases, the less the number of lines of code, the higher the efficiency. Let's take a look at the "simple" example below:

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";}

How difficult is the if condition? Imagine that the person who wrote this code did not follow the 1st- Add comments to code.

Isn't it easier to break the if condition into two if statements? Now let's take a look at the modified Code:

 1 if(newStatusCode.equals("SD") && (sellOffDate == null ||  2 todayDate.compareTo(sellOffDate)<0 || (lastUsedDate != null &&  3 todayDate.compareTo(lastUsedDate)>0))){ 4         newStatusCode = "NYP"; 5 }else  6 if(newStatusCode.equals("OBS") && (OBSDate == null ||  7 todayDate.compareTo(OBSDate)<0)) 8 { 9         newStatusCode = "NYP";10 }

Isn't the readability better? Indeed, we write repeated statements. Indeed, we write one more if and two braces, but the code is indeed easier to read and understand!

4. Do not use hard encoding (no hard coding please ).-Due to time constraints, developers always forget or deliberately ignore this article. However, the other possibility is that we will not be in the "time-tight" dilemma by following this rule. How long does it take to define a static final variable and add a line of code? For example:

 

 1 public class A { 2      3         public static final String S_CONSTANT_ABC = "ABC"; 4      5         public boolean methodA(String sParam1){ 6              7             if (A.S_CONSTANT_ABC.equalsIgnoreCase(sParam1)){ 8                 return true; 9             }        10             return false;11         }12 }

 

Now, every time we need to compare the string "ABC" with a variable, we just need to referenceA.s _ constant_abc, instead of remembering what it is. It is also very convenient to modify this constant. You can change it to a place without having to search for it in all the code.

5. Do not invent your own framework (do not invent your own frameworks ). -To put it bluntly, thousands of frameworks already exist, and most of them are open-source. Many frameworks are perfect solutions and have been used in thousands of systems. We only need to pay attention to the latest popular frameworks, at least we should be familiar with them on the surface. The most successful and widely used example is the Struts framework. This open-source Web framework is an excellent choice for building a web system. Do not try to build your own struts version, will be exhausted. But you must remember the 2nd (the original article is "3rd", obviously not true) precepts-do not complicate simple things. If the system you want to develop has only three interfaces, do not use struts. for such a system, there is not enough things to be "controlled, so the author says there isn' t much "controlling" required ).

6. Say no to print rows or strings (say no to print lines and string concatenations ).-I know programmers like to use it everywhere for debugging convenience.System. Out. printlnAnd then I will delete it after talking to myself. But we often forget to delete these rows or do not want to delete them. We useSystem. Out. printlnWhy should I modify the code after testing? This may lead to the accidental deletion of a line of the code we need. Do not underestimateSystem. Out. println hazards, see the following code:

 1 public class BadCode { 2     public static void calculationWithPrint(){ 3         double someValue = 0D; 4         for (int i = 0; i < 10000; i++) { 5             System.out.println(someValue = someValue + i); 6         }     7     } 8     public static void calculationWithOutPrint(){ 9 10             double someValue = 0D;11             for (int i = 0; i < 10000; i++) {12                 someValue = someValue + i;13             }14         15     }16     public static void main(String [] n) {17         BadCode.calculationWithPrint();18         BadCode.calculationWithOutPrint();19     }20 }

The following table shows that,Calculationwithoutprint ()The method execution time is 0.001204 S. For comparison, the calculationwithprint () method actually needs an incredible 10.52 s execution!

(If you want to know how to make such a table, read another article "Java profiling with wsad" Java profiling with wsad)

To avoid CPU waste, the best way is to introduce a packaging method as follows:

 1 public class BadCode { 2      3         public static final int DEBUG_MODE = 1; 4         public static final int PRODUCTION_MODE = 2; 5      6     public static void calculationWithPrint(int logMode){     7         double someValue = 0D; 8         for (int i = 0; i < 10000; i++) { 9             someValue = someValue + i;10             myPrintMethod(logMode, someValue);11         }12     }13             14     public static void myPrintMethod(int logMode, double value) {15         if (logMode > BadCode.DEBUG_MODE) {    return; }16         System.out.println(value);    17     }18     public static void main(String [] n) {19         BadCode.calculationWithPrint(BadCode.PRODUCTION_MODE);20         }21 }

String connection is another method of CPU waste. See the following example:

 1 public static void concatenateStrings(String startingString) { 2         for (int i = 0; i < 20; i++) { 3             startingString = startingString + startingString; 4         } 5     } 6      7     public static void concatenateStringsUsingStringBuffer( 8 String startingString) { 9         StringBuffer sb = new StringBuffer();10         sb.append(startingString);11             for (int i = 0; i < 20; i++) {12                 sb.append(sb.toString());13             }14 }

The following table shows how to useStringbuffer0.01 s and 0.08 s for string connection, which one should be obvious.

 

7. Pay attention to the GUI ).-No matter how ridiculous it sounds, I have noticed that graphical user interfaces (guis) are equally important to commercial users as program functions and execution efficiency. GUI is critical to the success of applications. IT managers often ignore the importance of guis. Many companies do not hire web designers to save money, these designers have sufficient experience to design "user-friendly" application software. Java programmers have to rely on their limited hmtl knowledge. I have seen many "computer-friendly" rather than "user-friendly" applications. It is rare for developers who are proficient in software development and user interface development. If you are a Java programmer who is unfortunately assigned to Develop interfaces, follow the following three rules:

  1. Do not re-invent the wheel. Let's look at the interfaces that are similar to application systems.
  2. First, create a prototype. This step is critical. Customers like to see what they want in advance. You can also get feedback from them, instead of making things you don't like.
  3. Try on the user's hat. In other words, you can view your requirements from the user's perspective. For example, a statistical page can be displayed by page or by page. As a developer, paging is likely to be ignored, because it reduces a lot of trouble. From the customer's perspective, this is not a good solution, because the data may be several hundred rows.
8. Prepare the requirement document (always prepare document requirements) in advance ).-Each business requirement is recorded in the document. This may be realized in fairy tales, but it is hard to do it in reality. No matter how short the time is, no matter how close the deadline is, you must ensure that your business needs are recorded. Note: This concept is obviously contrary to agile development. You must think independently to identify right and wrong)

9. unit test, unit test, unit test (unit-test. Unit-test. Unit-test ). -I am not going to discuss details about how to perform unit testing. I just want to say that this is required. This is the most basic rule in programming, especially not to be ignored. If your colleagues can create a test plan for your code, it would be better. If you cannot, you have to do it yourself. Follow the following principles when preparing a unit test plan:

  1. Write unit test before coding
  2. Retained comments of unit test
  3. Unit testing is required for any "interesting" public method ("interesting" refers to methods other than the most common getter/setter methods, but the getter/setter method that contains its own content)

10. Remember: quality, not quantity (remember-quality, not quantity ).-Don't wait too late (unless necessary ). I know that sometimes I cannot get off work on time due to product problems, deadlines, or other emergencies. However, managers will not appreciate or reward you for waiting too late for general problems; they will appreciate you for quality work. If you follow the principles of the above columns, you will write more robust and less bug programs. This is what you should do most.

Conclusion

This article summarizes the 10 most important rules for Java programmers. It is not enough to know and follow them. We hope these codes will allow us to become more professional programmers.

 

 

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.