当前位置:网站首页>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 .
边栏推荐
- It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
- [source code] VB6 chat robot
- How to prevent malicious crawling of information by one-to-one live broadcast source server
- 2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
- Briefly understand the operation mode of developing NFT platform
- Cgb2201 preparatory class evening self-study and lecture content
- Fudan 961 review
- Gossip about redis source code 78
- C # basic knowledge (2)
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
猜你喜欢
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
2/14 (regular expression, sed streaming editor)
Enter MySQL in docker container by command under Linux
Idea set class header comments
[MySQL] classification of multi table queries
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
What is the Valentine's Day gift given by the operator to the product?
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
随机推荐
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
C summary of knowledge point definitions, summary notes
Gossip about redis source code 82
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
How to understand the gain bandwidth product operational amplifier gain
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
[about text classification trick] things you don't know
Alibaba cloud container service differentiation SLO hybrid technology practice
A treasure open source software, cross platform terminal artifact tabby
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Subgraph isomorphism -subgraph isomorphism
Unity shader visualizer shader graph
C # basic knowledge (1)
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
Op amp related - link
Docking Alipay process [pay in person, QR code Payment]
How can I get the Commission discount of stock trading account opening? Is it safe to open an account online
Live app source code, jump to links outside the station or jump to pages inside the platform