Code review best practices

Source: Internet
Author: User
Tags bug id javascript eval

The article is reproduced from:Open source Chinese communityHttp://www.oschina.net

Code review can help improve code quality and avoid bugs caused by code habits. The points listed below should serve as guidance for most code reviews. For Java applications, these suggestions should be considered as best practices.

Document
1. javadoc should be added in every class and method.
2. If a bug is fixed, add the Bug ID.
3. There must be explanations of shortcuts or complex logic.
4. If the code is made public, the copyright information must be added to each file header.
5. Complex HTML, JavaScript, and CSS should contain documents.

Function
1. If a similar logic is used multiple times, you should write it as a help class and then call it more.
2. You are encouraged to use APIs instead of writing code repeatedly to solve the same problem.
3. Emphasize the unit test of the Code.
4. Any newly added code should not destroy the existing code.
5. For Web applications, JSP should not contain Java code.

Security
1. No code can execute user input unless it has been escaped. This usually contains Javascript eval functions and SQL statements.
2. Prohibit IP addresses that submit many requests in a short time.
3. All classes, variables, and methods should have correct access domains.
4. Avoid using IFRAME whenever possible.

Performance
1. All Database and file operation handles should be closed when they are not needed.
2. The write method of SQL statements may cause a wide variety of performance.
3. Create immutable classes.
4. Similar logic code, use the if else statement as much as possible to achieve more reuse.
5. Avoid using heavy objects (heavy objects) whenever possible ).
6. For a web project, check whether appropriate image sizes, CSS Sprites, browser cache, and other technologies are used.
7. Information required globally is saved in application context.

Coding habits
1. unused variables to be deleted.
2. Use different catch statements for different exceptions instead of an exception to solve all problems.
3. For variables, the methods and classes should use the same naming method.
4. constants should be written in an independent constant class.
5. There should be no extra spaces at the end of each line of code.
6. Brackets, loops, and if statements should be in a uniform format.
7. Each separate method should not exceed 100 rows.
8. A separate statement should not exceed the visible area of the editor. It can be split into several lines.
9. The best way to check whether the string object is null or not is if ("". Equals (STR ))
10. If the class has many member variables and only a few variables need to be passed in during instantiation, it is best to use the static factory method instead of the overloaded constructor.
11. Add appropriate access control to the method, instead of all being public.
12. Follow the best practices and suggestions for frameworks used in the project, such as spring, struts, hibernate, and jquery.

Some of the preceding precautions can be completed using static code check tools, such as checkstyle, findbugs, and jtest.

 

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.