当前位置:网站首页>Suggestions for improving code quality
Suggestions for improving code quality
2022-07-03 23:51:00 【Manon Feifei】
Programmers are like craftsmen from all walks of life , All rely on their own works . So if you want to get high income and recognition in the industry, you must constantly learn to improve your business level . Here I have summarized some suggestions for programmers to improve code quality, hoping to be helpful to your advanced level .
1. Improve code reuse
Whether it's writing new code or refactoring old code , We must pay attention to improving the reusability of code . Many Xiaobai only care about the implementation of business functions when writing code , Often the code is written as a lump . The next time there is a similar requirement, it has to be rewritten , The previous code is not used at all , This leads to a lot of repetitive work . So when we write code, we must pay attention to the reusability of code , Divide the code effectively , Achieve high cohesion and low coupling . Independent modules thus decoupled , It can be reused directly in other similar businesses . Reusable code generally has the following two characteristics :
1. The code is decoupled independently , Independent of other modules
2. The code is stateless , Repeated calls have no side effects
2. Remove unnecessary dependencies
Many novices are writing code , habit Copy Code , Sometimes, in order to realize a small function, you need to go elsewhere Copy A lot of dependent header files come into the business , Whether these header files can be used or not . Over time, the code becomes smelly and long , Very difficult to maintain . When we were writing code , There must be a sense of simplification , The header file is used and then imported , Remove what you can't use . At the same time, the third-party library that cannot be used must be removed in time , This can not only improve the quality of the code, but also improve the efficiency of the code .
3. Multithreaded applications consider thread safety
In multithreaded applications, if the data is read-only , Generally, there is no problem , However, if multithreading is involved in writing data at the same time, thread safety should be considered , Prevent data from being written repeatedly during multithreading . Be sure to lock when synchronizing data with multiple threads , Prevent conflicts between threads , At the same time, we should also consider the performance problems caused by locking in multithreading process . At the same time, the synchronization problem between threads should be avoided as far as possible . When introducing multithreading, you should also consider whether it will cause thread deadlock .
4. Adopt the scheme of delayed allocation
Many people write code for convenience , Declare the instance of the dependent class directly in the member of the class , The advantage of this is that you don't need to control the life cycle of member variables , The disadvantage is that the performance becomes worse when the class is instantiated . So if a member variable of a class is complex , Initialization takes time , I suggest adopting the scheme of delayed allocation . Deferred allocation is to declare the pointer of the corresponding dependency in the member variable of the class , Then instantiate when calling . Destroy the modified object when destructing or destroying . And when the class is called , We must avoid frequent creation and destruction of objects, which is a serious loss of performance .
5. Reduce memory allocation 、 Copy and release frequency
If the function input parameter passes a complex or memory consuming object , It is recommended to pass the reference of the class , This can avoid the creation and destruction of temporary variables , So as to improve the efficiency of function execution . When writing code, we must try to reduce the frequency of memory allocation, copy and release , These are time-consuming operations , If you can use the previous cache , Use the previous cache as much as possible .
6. class 、 Method 、 Variable usage private Modifier
Improve class encapsulation , Avoid exposing redundant interfaces and member variables , Use... As much as possible private jurisdiction , Can improve code security .
7. Is there any sensitive information in the abnormal information
Most of the time, for the convenience of debugging , Some sensitive content will be output to the log in the exception information , Including the running logic of the program and some user's personal sensitive information . Little imagine , This information may be obtained by some people with ulterior motives and used to destroy our system . So when releasing the program again , Be sure to check whether there is sensitive information in the abnormal information .
8. Control the permission domain of the code
Some code is privileged when writing products , For example, some codes can operate the server-side database , Delete the database . Some codes have administrator privileges , It can affect the user's operating system . When writing this kind of code , We must be very careful , Try to limit the privileged code to a limited range , Control it . At the same time, the authorization of other function call modules must adhere to the minimum principle , Prevent abuse of authority .
9. Pay attention to the handling of sensitive information
In product development , We must protect sensitive information , Prevent information leakage .
What information is sensitive ?
1. Personal sensitive information
Identity information 、 Privacy information 、 Business information, etc
2. Business sensitive information
Employee information 、 Business data 、 Process information, etc
Who has access to the corresponding sensitive information ?
The principle of minimum Authorization , The operation modules of sensitive information should be reduced as much as possible , Reduce the authorization of sensitive information .
Is there any sensitive information in the application log ?
Do not output sensitive information in the log , There are often problems in this aspect .
Does object serialization exclude sensitive information ?
Previously, there was a problem of leakage during the serialization and sending of sensitive information , So try to avoid sending user sensitive information through serialization .
Is there any special treatment for the storage of sensitive information ?
For example, the user's password cannot be stored in clear text , Try to store the hash value of the password with salt .
Sensitive information must be cleared in time when it is not used .
10. Check the safety and effectiveness of data
When there is no verification point for external data , We must verify the data on the transmission path , Including security verification and validity verification , Prevent unauthenticated data from invading the system and causing damage to the system .
边栏推荐
- How to make recv have a little temper?
- Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
- Ramble 72 of redis source code
- After the Lunar New Year and a half
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
- Common mode interference of EMC
- A method to solve Bert long text matching
- "Learning notes" recursive & recursive
- Gorilla/mux framework (RK boot): add tracing Middleware
猜你喜欢
![[source code] VB6 chat robot](/img/89/46b67f627c8257eaddc70a247c9ba5.jpg)
[source code] VB6 chat robot
![[about text classification trick] things you don't know](/img/c0/fdb04f6b31f1dba2658c2430dc4036.jpg)
[about text classification trick] things you don't know

The first game of the new year, many bug awards submitted

China standard gas market prospect investment and development feasibility study report 2022-2028

Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at

How to quickly build high availability of service discovery

2022 system integration project management engineer examination knowledge points: software development model
![[network security] what is emergency response? What indicators should you pay attention to in emergency response?](/img/ff/c733ffbb922760910ab09af3ae2886.jpg)
[network security] what is emergency response? What indicators should you pay attention to in emergency response?

How to prevent malicious crawling of information by one-to-one live broadcast source server

Common mode interference of EMC
随机推荐
Smart fan system based on stm32f407
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
Gossip about redis source code 73
NLP pre training technology development
P1629 postman delivering letter
Tencent interview: can you find the number of 1 in binary?
AI Challenger 2018 text mining competition related solutions and code summary
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Current detection circuit - including op amp current scheme
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
Cgb2201 preparatory class evening self-study and lecture content
Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
"Learning notes" recursive & recursive
Selenium library 4.5.0 keyword explanation (I)
Investment demand and income forecast report of China's building ceramics industry, 2022-2028
Schematic diagram of crystal oscillator clock and PCB Design Guide
How about opening an account at Hengtai securities? Is it safe?