当前位置:网站首页>Code neatness (2)
Code neatness (2)
2022-07-28 06:41:00 【Mr.QSheng】
1.Consolidate Duplicate Conditional Fragments( The condition judgment of merging repetition )
principle : In the branch of conditional judgment , Extract the same code segment , Transfer to the outside
effect : Reduce code redundancy , Split the changing code and the unchanging code
2.Remove Control Frag( Remove Control Flag )
principle : Once the condition judgment is established in the loop statement , immediately break,tontinue,return End cycle , Or skip directly to the next cycle
effect : Jump out of the cycle in time , Avoid unnecessary and invalid loops
3.Replace Nested Conditional with Guard Clauses( Replace nested conditional expressions with guard statements )
principle : Distinguish special judgment conditions from ordinary judgment conditions , Improve the particularity of special judgment . Code can be simplified in other ways
use : Pay more attention to special condition judgment , Distinguish from ordinary judgment conditions , Improve code clarity
4.Replace Conditional with Polymorphism( Replace conditional expression with polymorphism )
principle : Use polymorphism or state Instead of conditional branching
effect : Omit the conditional judgment Branch , Improve code readability , Expansibility
5.ename Method()
principle : Define the function name according to the function function
effect : Emphasize the meaning of function , Increase code readability .
6.Separate Query from Modifier
principle : When a function combines multiple functions , Such as query and modification . Try to separate them and modify the references
use : When a function combines multiple functions , Such as query and modification . Separate them to improve code readability
Parameterize Method( Let the function take parameters )
principle : If several functions do similar work , However, due to a small number of values, it is slightly different , Try to fuse into a function , Reduce code repeatability , Through parameters
To deal with those changes
effect : Reduce code repeatability
Replace Parameter with Explicit Methods( Replace parameters with explicit functions )
principle : If the behavior of a function completely depends on the parameter value and takes different behavior, an independent function can be established for each possible value of the parameter
effect : Clarify the meaning of the function
Preserve Whole Object( Keep objects intact )
principle : If a function receives some values in an object as parameters , You can pass its entire object as a parameter
effect : Function takes an object as an argument , Avoid too long parameters , It also improves the simplicity of the code , And expansibility
Replace Parameter with Method( Replace parameters with functions )
principle : Write the reference to get the parameter value into the function
effect : If the function can get the parameter value in other ways , Then he should not get the value through parameters , Reduce the length of function parameters
Remove Setting Method( Remove the set value function )
principle : Delete the set value function of this variable , Initialize it through the construction method
effect : If a variable is initialized, there is no need to assign other values , Then the set value function should be deleted
Hide Method( Hide functions )
principle : Set the function to private type
effect : If a function does not need to be referenced by other classes , Then he should be hidden , Avoid exposing too many interfaces to reduce the readability of the program
Replace Construcot with Factory Method
principle : Create factory function create object
effect : You want to create objects more than just simple construction actions , You can replace constructors with factory functions
From Template Method( Shape template functions )
motivation : The two functions perform roughly similar operations in the same execution order , But the operation is not exactly the same , You can move the sequence of operations to the superclass , With the help of
Polymorphism keeps the difference of each operation
principle : Analysis function , Extract the same part to the superclass , Maintain the difference of functions through polymorphism
effect : Mold template functions to extract common parts , Fill different parts in subclasses , Reduce code duplication
Replace Inheritance with Delegation( Replace inheritance with delegation )
motivation : A subclass is only a part of the superclass interface , Or you don't need inherited data at all , It can be entrusted , Eliminate inheritance
principle : Replace the inheritance relationship by adding a reference to the superclass
effect : Clear code intent , Avoid abusing inheritance
Tease Apart Inheritance( Comb and decompose the inheritance system )
motivation : A system of succession has two responsibilities at the same time
principle : Analyze the inheritance system , Cut modules with different meanings , Generate a new inheritance system , Delete the parts with different meanings in the original inheritance system , And add the reference field of the new inheritance system
effect : The meaning of distinguishing classes , Is the code more extensible , More clarity
Extract Hierarchy( Refine the inheritance system )
motivation : A class does too much work , Part of it is completed by a large number of conditional expressions
principle : Refine the inheritance system according to different conditions , And instantiate the corresponding object for different situations through factory functions
effect : Optimize code structure , Improve code extensibility , Readability
Sparate Domain from Presentation( Put the field and the expression / Display separation )
motivation : Some presentation layers contain the code of domain logic, that is, model , This leads to high code coupling
principle : Distinguish presentation layer and domain logic and be independent of each other , For public fields , Add and listen to different field values on both sides at the same time
effect : Reduce code coupling , Improve code clarity , Enhance domain modularity
Convert Procedural Design to Object( Turn process design into object design )
motivation : Procedural changes can easily lead to unclear code , Difficult to understand and expand
principle : By using object-oriented , Encapsulate dumb data , Refine classes and functions
effect : Sort out the code structure , Improve code readability , Expansibility .
introduce Null Object( Introducing empty objects )
principle : introduce Null object ,Null Object inherits from source class ,Null Object to implement the corresponding processing of each function when the source class is empty
Encapsulate the non empty check of the object in getobject in , If it is blank, return Null object
effect : Reduce the repetition rate of non empty object inspection in code
expand :Special Case Pattern ( Special class )Null Objects may require different functions in different situations , Thus, multiple Null Objects cope with different situations
Introduce Parameter Object( Introduce parameter object )
principle : Encapsulate multiple parameters as parameter objects , Only constructors are provided to modify the contents of objects , Modify multiple parameters of the function to a new instance of the parameter object and assign values
effect : Shorten parameter column , Improve code readability , And consistency ( Multiple functions take the same parameters )
Move Field ( Move fields )
principle : Add the same field value in another class , And add get(),set() function . It is allowed to delete the corresponding field in the source class
effect : For a field , It is used by more functions in another class besides its resident class , You can consider moving this field ( Of course, you can also choose the move function )
Move Method( Move function )
principle : Add a function name with the same name to the target class , Modify call mode
effect : For a function, it is used by more functions in a class other than the class in which it resides , You can consider moving this function
Parameterize Method( Let the function take parameters )
effect : Multiple similar functions , The behavior is different due to several values , Refine the same , And corresponding different behaviors through parameter processing
Preserve Whole Object( Keep objects intact )
principle : If a function requires several values in an object , Instead of splitting and passing several values in the object , It's better to pass the whole object
effect : Improve code maintainability , Readability
Pull Up Constructor Body( Move the constructor body up )
principle : Subclass inherits parent , Extract the same fields that need to be assigned in the constructor , call super Function assigns values to the same fields
effect : Reduce code redundancy , Refining code
Pull Up Fiesd ( Field move up )
principle : In a subclass , If there is a field meaning , The same effect , It can be referred to the definition in the parent class
effect : Reduce code redundancy , Refining code , Improve code readability
Pull Up Method( Function upwards )
principle : In a subclass , If there is a function meaning , The same effect , It can be referred to the definition in the parent class
effect : Reduce code redundancy , Refining code , Improve code readability
Push Down Fied ( Field down )
principle : In the parent class , If there is a special field , Only used in a subclass , It can be defined in the corresponding subclass
effect : Increase code readability , Maintenance
summary
The above is the content of this sharing . It is also the first issue of my blog . While sharing, you are also familiar with the document writing process . The follow-up meeting will start to share shader Related content .
边栏推荐
- ubunut 服务器上传下载文件
- What is the best and most cost-effective open headset recommended
- OpenGL quick configuration method
- OJ 1129 分数矩阵
- 藏宝计划TPC系统开发Dapp搭建
- 2022年七夕礼物推荐!好看便宜又实用的礼物推荐
- 2022-05-15 基于jwt令牌token
- [PTA----树的遍历]
- Leetcode 刷题日记 剑指 Offer II 050. 向下的路径节点之和
- What's a good gift for your girlfriend on Chinese Valentine's day? Boys who can't give gifts, look!
猜你喜欢
随机推荐
【动态规划--买卖股票的最佳时期系列3】
Current learning progress
pyppeteer 下拉 selenium下拉
OJ 1242 大一上之初出茅庐
中国剩余定理 个人理解
到底什么是Hash?(量化交易机器人系统开发)
NFT数藏盲盒+模式系统开发
set_ multicycle_ path
2022-06-07 responsebodyadvice caused the spring frame problem in swagger
valgrind工具
QT implementation outputs relevant information to log file
2022-06-07 六.日志实现
OpenGL development environment configuration [vs2017] + frequently asked questions
气传导蓝牙耳机怎么样、最值得入手的气传导耳机
Get the current directory in QT
Execjs call
MFC uses the console to print program information
Use and safe shutdown of qthread thread in QT
刷题记录----二叉树的层序遍历
NFT data storage blind box + mode system development

![[队列,栈的简单应用----包装机]](/img/bc/617b1eb35558c4f948018f593a1de5.jpg)







