Top 10 Java developers

Source: Internet
Author: User
Tags one more line

There are many standards and practices that can be applied to Java developers. But here we will talk about the basic principles that every Java developer must stick.

1. Add comments to the code. Although everyone knows this, sometimes they do not consciously forget to perform it. Do you "forget" and add comments today? Although Annotations do not contribute much to the functions of the program, after a period of time, for example, two weeks later or longer, you can look back at your own code, maybe you can't remember what it is. If the code is yours, it is lucky. Unfortunately, of course, most of the time it is others' misfortune. Many times Everyone is writing code for the company, the person who writes the code may have left the company early, but don't forget to say that there are some things that come and go, for others, and for ourselves. Please add comments to your code.

2. Do not complicate things. Programmers sometimes always come up with complicated solutions for simple problems. For example, they can import ejbs to programs with only five users and implement frameworks that are not needed by programs ), such as attribute files, object-oriented solutions, and multithreading. Why? Maybe we don't know if it will be better, but doing so may help us learn new things or make ourselves more interested. If you don't know why, I suggest you consult experienced programmers. If you want to do this for your own purposes, please make yourself more professional.

3. Keep in mind that "Less is better (less is more) is not always right ". Although code efficiency is very important, writing less code in many solutions does not improve the efficiency of the Code. See the following 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 ";
}

Can you understand what the if Condition Statement is? Can you figure out who wrote this code? If we divide it into two independent if statements, isn't it easier to understand? The modified code is as follows:

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

Is it easier to read the code? Here, we only add an IF and two curly braces, but the readability and comprehensibility of the Code are greatly improved.

4. Do not use hard encoding. Developers often "forget" or ignore this because sometimes the development schedule is too tight. How long does it take to write one more line of code that defines static variables?

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 you need to compare "ABC" with other variables, you don't have to remember the actual code, just reference a.s _ constant_abc directly, and when you need to modify it in the future, you can also modify the source code one by one.

5. Do not "CREATE" your own framework ). Specifically, thousands of frameworks exist, and most of them are open-source. These frameworks are excellent solutions and can be used in daily program development, we only need to use the latest versions of these frameworks. At least we should keep up with the situation on the surface. The most obvious example widely accepted is struts. This open-source Web framework is very suitable for Web-based applications. Do you want to develop your own struts? Save some effort and look back at article 2-Don't complicate things. In addition, if the program being developed has only three windows, do not use struts. For such programs, there is no need for so much "control ".

6. Do not use println or string connection. For debugging convenience, developers prefer to add system in all possible places. out. println may remind you to go back and delete them, but sometimes you often forget to delete them or are unwilling to delete them. Since system. out. println is used for testing. Why should we keep them after the test? Because it is very likely that useful code will be deleted during the deletion, so we should not underestimate the system. out. println hazards. Please refer to 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 ();
}
}

From the test, we can find that the method calculationwithoutprint () takes 0.001204 seconds for execution. As a comparison, the method calculationwithprint () takes 10.52 seconds for execution.

To avoid wasting CPU time, the best way is to introduce the following packaging method:

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 addition, string connection is also a waste of CPU time, please refer to the following sample code:

Public static void concatenatestrings (string startingstring ){
For (INT I = 0; I <20; I ++ ){
Startingstring = startingstring + startingstring;
}
}
Public static void concatenatestringsusingstringbuffer (string startingstring ){
Stringbuffer sb = new stringbuffer ();
SB. append (startingstring );
For (INT I = 0; I <20; I ++ ){
SB. append (sb. tostring ());
}
}

In the test, we can find that the stringbuffer method takes only 0.01 seconds to complete execution, and the connection method takes 0.08 seconds. The choice is obvious.

7. focus more on the GUI (User Interface ). It has been repeatedly stressed that GUI is equally important to commercial customers as the functions and efficiency of a program. GUI is the most basic part of a successful program, and many IT managers often do not notice the importance of GUI. In real life, many companies may not hire web designers with rich experience in designing User-friendly interfaces to save money, at this time, Java developers can only rely on their own basic HTML skills and limited knowledge in this field. As a result, many developed programs are "computer friendly" and "user friendly ". Few developers are proficient in software development and GUI Design at the same time. If you are "Unfortunately" assigned to the responsible program interface in the company, follow these three principles:

1. Do not re-invent the wheel, that is, do not useless work. Existing programs may have similar interface requirements.

2. Create a prototype first. This is a very important step. Users generally want to see what they will use, and they can use this prototype to solicit users' opinions and then slowly change it to what they want.

3. Learn to think differently. In other words, it is to review the application requirements from the user's perspective. For example, a summary window can be cross-page or non-Cross-page. As a software developer, it may tend to be non-Cross-page, because it is simpler. However, from the user's perspective, you may not want to see hundreds of rows of data on the same page.

8. Document requirements are not relaxed. Every business needs must be documented. This may sound like a fairy tale, which seems difficult to implement in real life. What we need to do is to record every business requirement, no matter how long the development time is, no matter how long the deadline is approaching.

9. Unit Testing, unit testing, and unit testing. It is inconvenient to elaborate on what is the best method for unit testing. It just emphasizes that unit testing must be completed, which is also the most basic principle in programming. Of course, it would be best if someone helps you with unit testing. If not, do it by yourself. When creating a unit test plan, observe the following three basic principles:

1. Write unit tests before writing class code.

2. Record the code comments in unit test.

3. test all the public methods that execute key functions. This does not refer to the set and get methods unless they execute the set and get methods in their own unique ways.

10. quality, not quantity. Sometimes, due to product problems, deadline constraints, or unexpected events, the company often cannot get off work on time. However, in general, the company will not praise and reward employees for their frequent overtime work, companies only focus on high-quality jobs. If you comply with the first nine principles, you will find that the code you write has few bugs and high maintainability, And the quality is greatly improved.

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.