Top 10 Java developers

Source: Internet
Author: User

 

Author: Aleksey Shevchenko has many standards and best practices for Java developers. 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 codeEveryone 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 thingsI 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-"less is more ( 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, 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:
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. Do not have hard codeDevelopers 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 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 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. 5. Do not invent your own FrameworksThousands 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 -- Don't 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 System. Out. printlnAnd 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. printlnWhen the test is complete, why do we still have access to them? We may delete a line of code that we actually need, just because you underestimate it. System. Out. printlnFor the damage caused, 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 following table, you can see Calculationwithoutprint ()It took 0.001204 seconds to run the method. In comparison Calculationwithprint ()The method took an astonishing 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 package 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);
                               }
}
As shown in the following figure StringbufferThe method takes only 0.01 seconds to run, while the method that uses string addition takes 0.08 seconds to run. The selection is obvious. 7. Follow GuiNo 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 requirementsEvery 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, Unit TestingI will not discuss in depth the details about what is the best way to perform unit tests on your code. What I'm going to say is Unit test is required. 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. SummaryIn 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. About the authorAleksey Shevchenko has more than seven years of experience in object-oriented programming. He is now working on IT solutions for Wall Street, manufacturing and publishing industries.

Related Article

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.