The old driver tells you how to practice high-quality Java code?

When it comes to programmers, the first thing that comes to mind must be "code farmers". Yes, we are high-quality "code farmers" with high output. We have the logical thinking that surpasses ordinary people and the ability to analyze and discriminate. Of course, we There are also good coding standards. After all, I rely on code to support my family. I am most afraid of receiving a project or adding new functions to the project halfway. I found that the comments I wrote before were very incomplete, the code was also very untidy, and some places wrote '' nests. It's rotten, and it looks very uncomfortable.

What is the role of code coding standards?
1. A good coding standard can reduce the maintenance cost of a software, and almost no software is maintained by the original developer in its entire life cycle; 
2. A good coding standard can improve the readability of the software
3. Good coding standards can maximize the efficiency of team development cooperation; 4. Long-term normative
coding can also allow developers to develop good coding habits , and even exercise more rigorous thinking;

How to make high-quality Java code?

  • name

A bad naming can be misleading, bad naming, and tangled for someone reading the code. A good name also helps a lot.

 

It is recommended that the variable named by the individual be longer, generally use the word as the full name, so that the code is easy to read, and some abbreviations do not know what the word it represents, except for common abbreviations such as id for identifier and org for organization.

 

When naming a method, it is best to let everyone know the name, and you can guess your function by seeing the name, without having to look at the comments of the method, or even read the source code to understand your function.

 

  • Notes

When writing a method, you can first explain the function and algorithm principle of the method. It will be very convenient for yourself or others to maintain your code in the future, and you can add comments and reminders to the error-prone parts.

 

use class type

When writing method parameters, use less combination of basic types, and use class types.

E.g:

Write a method queryUser(int age) to find users

The initial business requirement was to find users based on age. Later, the business rules changed. You may need to find users based on age and gender, so you changed it to queryUser(int age, intsex), assuming that 0 represents male. , 1 represents female (in fact, a better implementation is to use enumeration to represent male and female);

Maybe your business will change again one day, and you need to query based on age, gender, and home address, so you changed it to queryUser(int age, int sex, String address).

 

If the method you designed at that time was: the parameter passed in by queryUser(User user) was a User class, how great would it be, you didn't need to change the interface at all.

 

The cost of changing an interface in actual project development is still quite large. In order to achieve the purpose of clear levels and decoupling in actual project development, the background is divided into many layers. Action, business, and dao also have dao interfaces and implementations. , how many places are affected by an interface modification.

 

The originally designed interface passes the User object, so your code can simply add a few lines to achieve the goal, instead of modifying so many interfaces, and tangled while modifying.

 

  • Copy and paste less code

Don't stick the same code back and forth. It was really fast when you wrote it at that time, but it will be much slower when you need to modify it later.

 

What's even more terrifying is that you have to modify many places, and you only modify one place, but you think that everything is fine, and maybe a bug will pop up one day. These public code should be extracted into a class or a method.

 

  • Don't write too much code in one method

Writing a lot of code in one method is really convenient and fast. A better way is to decompose a large method into several small methods, and then call other sub-methods in the main method.

 

If all the logic is written in one method, when the requirements change, it will be much slower to modify it.

 

A small piece of logic code can extract a private method, and then call several private small methods in one method.

 

In this way, it is also easy for people who read the code to read the code. If the requirements change in the future, as long as these small logical code blocks of yours are recombined, they can meet new functions and can be reused.

 

  • Add design documents

When adding a new functional module, it is best to have a design document, first consider all aspects, and then code to implement it.

 

如果一开始就有个设计文档,能把方方面面都考虑周全,实现起来就容易多了,实现的代码还能优雅些。

 

为了达到最终的目的,可能中间要走些弯路,如果增加的功能多了,每次实现都走一些弯路,系统最终会变的臃肿不堪。

 

如果推倒重来,以前的功夫就都白费了,不光是编码,还有测试部门的测试,有时时间也不允许重构,再说了重构还有风险,这其中的代价还是挺大的。

 

所以新增功能一定要把需求搞清除,有个良好的设计文档,考虑周全了再编码实现。

 

最后在向SVN提交代码时先做个功能测试,然后没问题了,再做个codereview。

 努力的人,老司机今天的忠告,你记住了吗?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325262090&siteId=291194637