My Java optimization, specification summary

Source: Internet
Author: User

Code Article

1. Cycle in particular , the cycle of the body defined variable, constant ... it's all wrong.

2. Multiple if judgments can be replaced with map !!!

3. Avoid using else statements, alternative scenarios: Guardian statement, State mode or policy mode!!!

4. if judgment is nested within A for loop, operator substitution can be used. Achieve scalability and maintainability , eliminating the loop

5. Use the Commons tool class as much as possible in the project

6. parametric multi-write JavaBean to encapsulate

7. constants are divided according to the business, and the code is as direct as possible constants

8. only for non-stable code catch exception, abnormal from small to large to capture, respectively, processing

9. Write more comments, it is recommended to annotate each piece of business code!!! (Reading to write notes, writing articles to make a draft), the project lacks the necessary comments , concise and powerful comments to help the development of efficiency and other people's reading!!!

Repeat the code problem, extraction!!! Can copy on the resolute non-stick, duplicate code will lead to later changes in the amount of increased , easy to omit ; repeating code is critical , quality is particularly important. There are a lot of duplicate code in the project!!!

One . When the collection is initialized, try to specify the size of the set initialization to reduce the consumption

recommended for explicitly unused code, or add todo to clear it in time to avoid garbage

Final can increase the corresponding efficiency of the program

StringBuilder substitution String

The 13.23 design pattern corresponds to different scenarios, suggesting that the business in the project should be based on the design pattern and increase the maintainability and extensibility of the code.

use of redis,mq


Database

1. Building the table specification

Specification for Table name field names

the stored strings are almost equal, using Char

Fields and comments remain consistent forever

Decimal Type

suitable character storage length (save tablespace) tinyint smallint int and bigint selection

fields and comments remain consistent forever

length basic string, using char fixed-length string type

2.sql Optimization

for static tables, you can add redundant fields to related tables, reduce correlated queries , and improve performance

SQL statements all uppercase, reducing parsing costs

any longer SQL statement has an optimized space , and the longer SQL statements are optimized as much as possible

3. Index

No unique index ------- dirty data, inefficient

Highest-differentiated and equal sign on the far left

Optimize multi-paged scenarios with deferred correlation or subqueries

Select a.* from table 1 A, (SELECT ID from table 1 where condition LIMIT 100000,20) b where A.id=b.id

The associated field needs to be indexed when multiple tables are associated

Index length and sensitivity a pair of contradictory bodies, need to establish a reasonable index length

Using search engines instead of fuzzy queries, the national network seems to be doing

Use the overlay index for query operations to avoid table operations and improve efficiency. Application: When querying the specified column data!!!

The order by is finally part of the combined index, and the order of the indexes must be noted

Sq index target reached ref level

...

4.Mybatis

1. do not use * as the query field list, reduce parsing , Avoid inconsistencies

Select J. * From Shop_store_joinin

2. do not write a chatty data update interface , passed into the POJO class. For example, the merchant settled in, just update a state, but all the fields have been updated, there is a risk , and then the extra judgment and Operation , three increased storage


My Java optimization, specification summary

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.