What "disciplines" are there that Java programmers must abide by?
1. Add comments to your code. Everyone knows this, but not everyone does. How many times have you "forgot" to add comments? Indeed, comments will not add any function functionality to your program. But how many times have you seen the code written 2 weeks ago and you can't remember what it does? You are lucky that those uncommented codes are written by yourself and you will still have a residual impression in your mind. Unfortunately, most of the time, the code was written by someone else, and that person is likely to have left the company. There is a proverb that goes well: "There are coming and going, mutual benefit", so programmers should be considerate of each other (and yourself) and comment on your code.
2. Don't complicate things. I've done this before, and I believe you are the same. Developers tend to use complex methods to solve simple problems. We introduced EJB in a system with only 5 users to implement a set of frameworks for an application that does not require a framework, using attribute files, object-oriented solutions, and using threads, but these are not needed at all. Why did this? Some may not know there is a better solution, but others may do it on purpose to learn new things, or simply because it is fun. For those who don’t know a better solution, listen more to the advice of experienced programmers. For those who complicate designs purely for personal purposes, I suggest you be more professional.
3. Remember - Keep in Mind "Less is more" is not always better. High-efficiency code is a good thing, but in many cases, the less efficient code line is not efficient The higher it is. See the following "simple" example:
if(newStatusCode.equals("SD") && (sellOffDate == null || todayDate.compareTo(sellOffDate)<0 || (lastUsedDate != null && todayDate.compareTo(lastUse dDate)>0)) ||(newStatusCode.equals ("OBS") && (OBSDate == null || todayDate.compareTo(OBSDate)<0))){ newStatusCode = "NYP"; }How difficult is it to point out what this if condition is? Imagine again that the person who wrote this code did not follow Article 1 - add comments to the code.
Isn't it easier to decompose if conditions into 2 if statements? Now let's look at the modified code:
if(newStatusCode.equals("SD") && (sellOffDate == null || todayDate.compareTo(sellOffDate)<0 || (lastUsedDate != null && todayDate.compareTo(lastUse dDate)>0))){ newStatusCode = "NYP "; }else if(newStatusCode.equals("OBS") && (OBSDate == null || todayDate.compareTo(OBSDate)<0)) { newStatusCode = "NYP"; }Isn't this better readability? Indeed, we wrote repeated statements; indeed, we wrote one more if and two braces; but the code is indeed easier to read and easier to understand!
4. Don't "hard coding please". Due to the tight time, developers will always forget or deliberately ignore this item. However, another possibility is that by following this precept, we will not fall into the dilemma of "time-urgent". How long does it take to define a static final variable and add a line of code? for example:
public class A { public static final String S_CONSTANT_ABC = "ABC"; public boolean methodA(String sParam1){ if (A.S_CONSTANT_ABC.equalsIgnoreCase(sPara m1)){ return true; } return false; } }Now, every time we need to compare the string "ABC" with a variable, we just refer to A.S_CONSTANT_ABC without remembering what it is. Modifying this constant is also very convenient. Just change it in one place without having to look it up in all codes.
5. Don't invent your own frameworks. It's no exaggeration to say that there are already thousands of frameworks, most of which are still open source. Many frameworks are extremely perfect solutions and have been used in thousands of systems. We just need to pay attention to the latest popular frameworks and at least be familiar with them on the surface. One of the most successful and widely used examples is the Struts framework, which is an open source web framework that is an excellent choice for building web systems. Don't try to construct your own version of Struts, you will be exhausted. But you must remember the precepts of Article 2 (translator's note: the original text is "article 3", obviously not correct) - Don't complicate simple things. If the system you want to develop only has 3 interfaces, don't use Struts. For such a system, there is not enough things that need to be "controlled" (Translator's note: Struts divides the interface into MVC, C is the controller, so the author said there isn 't much "controlling" required).
6. Say no to Print lines and String Concatenations. I know that for the convenience of debugging, programmers like to use System.out.println everywhere, and then delete it after saying it to themselves. But we often forget to delete these lines or are unwilling to delete them. We use System.out.println for testing. Why do we still need to change the code after the test? This is likely to lead to a line of code we need accidentally deleted. Don't underestimate the harm of System.out.println, see the following code:
public class BadCode { public static void calculationWithPrint(){ double someValue = 0D; for (int i = 0; i < 10000; i++) { System.out.println(someVal ue = 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.calculationWi thPrint(); BadCode.calculationWithOutPrint( ); } }As can be seen in the table below, the execution time of the calculationWithOutPrint() method is 0.001204 s. In comparison, the calculationWithPrint() method actually requires an incredible 10.52 s to execute!
(If you want to know how to make a table like this, please read another article "Java Profiling with WSAD" Java Profiling with WSAD)
To avoid CPU waste, the best way is to introduce a wrapper method, as follows:
public class BadCode { public static final int DEBUG_MODE = 1; public static final int PRODUCTION_MODE = 2; public static void calculationWithPrint(i nt 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.DEBU G_MODE) { return; } System.out.println(value); } public static void main(String [] n) { BadCode.calculationWithPrint(BadCode.PRODUCTION_MODE); } }String connection is another way to waste CPU, see the following example:
public static void concatenateStrings(String startingString) { for (int i = 0; i < 20; i++) { startingString = startingString + startingString; } } public static voi d concatenateStringsUsingStringBuffer( String startingString) { StringBuffer sb = new StringBuffer(); sb. append(startingString); for (int i = 0; i < 20; i++) { sb.append(sb.toString()); } }From the table below, we can see that using StringBuffer only costs 0.01 s while using String connection requires 0.08 s. It should be obvious which one to choose.
7. Pay attention to the GUI. No matter how ridiculous it sounds, there is one thing I have noticed many times: a graphical user interface (GUI) is as important to business users as program functionality and execution efficiency. GUI is crucial to the success of your application. IT managers often ignore the importance of GUI. Many companies do not hire web designers to save money, and these designers have enough experience to design "user-friendly" application software. Java programmers have to rely on their limited HMTL knowledge. I've seen a lot of applications that are "computer-friendly" rather than "user-friendly" and are rare for developers who are proficient in software development and user interface development. If you are a Java programmer who is unfortunately assigned to do interface development, you have to follow the following 3 rules:
a. Don't reinvent the wheel. Go to see the interfaces similar to the application system.
b. Create a prototype first. This step is very critical. Customers like to see what they want to use in advance. You can also get their feedback instead of working hard to create something that customers don’t like.
c. Try the user's hat. In other words, view requirements from the perspective of users. For example, a statistical interface can be paged or not. As a developer, it is likely to ignore paging because it will reduce a lot of trouble; from the customer's point of view, this is not a good solution, because the data can be as many as hundreds of lines.
8. Prepare Document Requirements in advance. Each business requirement is recorded in the document. This can be achieved in fairy tales, but it is difficult to do in reality. No matter how tight the time is, no matter how close the deadline is, you must make sure that business needs are recorded. (Translator's note: This concept is obviously contrary to agile development. Everyone should think independently and distinguish right from wrong)
9. Unit test, unit test, unit test (Unit-test. Unit-test. Unit-test). I'm not going to discuss the details of how to unit test, I just want to say that this has to be done. This is the most basic rule in programming, and it cannot be ignored. If your colleagues can create a test plan for your code, it would be better; if not, do it yourself. When doing unit test plans, follow the following principles:
a. Write unit test before encoding
b. Keep the comments for unit tests
c. Unit testing is required for any "interesting" public method ("interesting" means a method other than methods like the most common getter/setter, but contains its own content.)
10. Remember: quality, not quantity. - Don't stay too late (unless it is necessary). I know that sometimes I can't get off work on time due to product problems, deadlines or other emergencies. But the manager will not appreciate or reward you for being too late for general problems; they will appreciate you for quality work. If you follow the principles of the column above, you will write more robust and bug-free programs. This is what you should do the most.
This article summarizes the 10 most important rules that Java programmers should pay attention to. Just knowing is not enough, follow them. I hope these rules will enable us to become more professional programmers.