This article describes the top ten commandments that Java developers need to know. Share it with everyone for your reference, the details are as follows:
As a Java developer, improving the quality and maintainability of your own code is a constant topic. I saw this article online and used it to encourage myself.
There are many standards and best practices for Java developers. This article lists the 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 tragic ending.
1. Add comments to your code
Everyone knows this but somehow forgets to follow it. Count how many times have you "forgot" to add an annotation? It's true: comments make no substantial contribution to the program's functionality. However, you need to go back to the code you wrote two weeks ago again and again, maybe for a lifetime, and you must not remember why the code behaves like this. If these codes are yours, you're relatively lucky. Because it might bring back memories. But unfortunately, a lot of the time, the code belongs to someone else, and there's a good chance he's left the company.
2. Don't complicate things
I've done it before, and I'm sure everyone has done it. Developers often come up with a solution to a simple problem. We introduced EJBs for an application with only 5 users. We use a framework for an application that doesn't even need it. We added properties files, object-oriented solutions, and threads to the application, but it didn't need them at all. Why do we do this? Some of us do it because we don't know how to do it better, but some of us do it to learn new knowledge and make the application more interesting for ourselves.
3. Remember – “less is more” is not always good
Code efficiency is a great thing, but in many cases, writing fewer lines of code doesn't make that code more efficient. Please 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 want to ask: Is it easy to tell what the if condition of the above code wants to do? Now, let's assume that whoever wrote this code failed to follow rule number one - add comments to your code.
Wouldn't it be simpler if we split this condition into two separate if statements? Now, consider the following corrected 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";}Wouldn't it be better readable? Yes, we repeated the statement condition. Yes, we have an extra "IF" and two extra pairs of parentheses. But the code is better readable and understandable.
4. Please no hard code
Developers often consciously forget or ignore this rule because we, as usual, are in a hurry. If we follow this rule, we may fall behind schedule. We may not be able to end our current state. But how much time would it cost us to write an extra line of code defining static constants?
Here's an example.
public class A { 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 need to compare the string "ABC" to some variable, we only need to reference S_CONSTANT_ABC instead of remembering what the actual code is. It also has the benefit of making it easier to modify a constant in one place, rather than looking for it in all your code.
5. Don't 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 used in thousands of applications. You need to keep up with these new frameworks, at least superficially. Among these excellent and widely used frameworks, one of the best and most direct examples is Struts. Of all the frameworks you can imagine, this open source web framework is a perfect candidate for web-based applications. But you must remember the second rule - don't complicate things. If you develop an application with only three pages - please, don't use Struts. For such an application, there is nothing to "control" the requests.
6. Don't print lines and add strings
I know that for debugging purposes, developers like to add System.out.println everywhere we see fit, and we tell ourselves that we will delete this code later. But we often forget to delete these lines of code, or we simply don't want to delete them. We use System.out.println to test. After we complete the test, why can we still access them? We may delete a line of code that we actually need simply because you underestimated 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(); }}In the table below, you can see that the calculationWithOutPrint() method took 0.001204 seconds to run. In comparison, running the calculationWithPrint() method took an astonishing 10.52 seconds.
(If you don't know how to get a table like this, please see 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, like the following
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) ; }}In the figure below, you will see that the method using StringBuffer only took 0.01 seconds to execute, while the method using string addition took 0.08 seconds to run. The choice is obvious.
7. Follow the GUI
No matter how ridiculous this sounds, I will say it again and again: GUI is as important to business customers as functionality and performance. The GUI is an essential part of a successful system. (However), IT magazines often tend to ignore the importance of GUIs. Many organizations save money by not hiring designers who have extensive experience in designing "user-friendly" GUIs. Java developers have to rely on their own knowledge of HTML, but their knowledge in this area is very limited. I've seen a lot of apps like this: they are "computer friendly", not "user friendly". I rarely see a developer who is proficient in both software development and GUI development. If you are the unlucky developer assigned to develop a user interface, you should follow these three principles:
1. Don’t reinvent the wheel. Look for existing systems with similar user interface requirements.
2. First create a prototype. This is a very important step. Customers like to see what they are going to get. It's also great for you because you get their feedback before you go all out and make a user interface that's going to make them angry.
3. Wear the user’s hat. In other words, examine the application’s requirements from the user’s perspective. For example, whether a summary page should be paginated. As a software developer, you tend to ignore pagination in a system because it leaves you with less development complexity. However, this is not the best solution from a user's perspective because the summarized data will have hundreds or thousands of rows.
8. Always prepare documented requirements
Every business requirement must be documented. This may be true in some fairy tales, but it is not possible in the real world. No matter how tight the time is for your development, or whether the delivery date is coming soon, you must always know that every business requirement is documented.
9. Unit testing, unit testing, unit testing
I won't go into the details of what is the best way to unit test your code. What I'm going to say is that unit testing has to be done. This is the most basic rule of programming. This is the most important of all the laws above that cannot be ignored. It's best if you have colleagues who can create and test unit tests for your code. But if no one does it for you, then you have to do it yourself. When creating your unit test plan, follow these rules:
1. Write unit tests before writing code.
2. Write comments in unit tests.
3. Test all public methods that perform "interesting" functions ("interesting" means methods that are not setters or getters unless they perform 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). I understand that sometimes, product issues, tight deadlines, and unexpected events can prevent us from leaving get off work on time. However, under normal circumstances, the manager will not appreciate and reward employees who leave work too late. He appreciates them because of the quality of the products they produce. If you follow the rules I gave above, you will find that your code has fewer bugs and is more maintainable. And that's the most important part of your job.
Summarize
In this article, I give ten important rules for Java developers. It is important not only to know these rules, but also to follow them during the coding process. Hopefully these rules will help us become better programmers and professionals.
I hope this article will be helpful to everyone in Java programming.