当前位置:网站首页>Method of code refactoring -- Analysis of method refactoring
Method of code refactoring -- Analysis of method refactoring
2020-11-06 21:35:00 【Irving the procedural ape】
The method of code refactoring —— Method reconstruction analysis
Intro
Want to write better code , You need to be alert to bad smells in your code , Today I want to write an article about how to analyze whether your method needs to consider refactoring
A method usually consists of three parts , Input (Input), Output (Output), Method body (Method Body), We will analyze whether a method should consider refactoring from these three aspects
Input
Method input is the parameter of the method , Generally speaking, the parameters of a method can be basically controlled in 7 Within a ( For reference only , You can measure it by yourself ,SonarQube
The default method has up to seven parameters ), If your method has too many parameters , You may need to consider refactoring a method parameter , The usual practice is to encapsulate a separate model, Parameters as model Properties of .
Take a common example , Like a news list API, It can be very simple at first , Just one lastId
, One count
Two parameters , But as business needs increase , Many other parameters may be added , For example, the front end provides a keyword Full text search , Provide a sortBy Sort , Match the news headlines , Author name matches , Classification matching , Filter according to the release time and so on , In the end, there may be many parameters for this method
Usually I add a new one XxxRequest
Of model, Then replace the method parameter with this model, Then designate [FromQuery]
That's all right. , You can compare the difference before and after a modification , Is the latter way more refreshing
Task<IActionResult> List(int lastId, int count, string title, string author, string keyword, int categoryId, string sortBy, DateTime? beginTime, DateTime? endTime)
Task<IActionResult> List([FromQuery]NewsListQueryRequest request)
Output
Output Is the return value of the method , Return as many specific types as possible , Try to avoid using Tuple
Other types , The return value of the method should have a clear meaning
Use specific Model Instead of Tuple Return value , Especially some public Of , Methods to be accessed externally should return specific types , although C# 7.2 Started to support named tuple, It's going to be a lot more friendly than before , Support to tuple Specify a name , But it's just compiler level , Actually, it's still Item1,Item2 ..., It is recommended to use specific ones model, More clearly
Body
Usually a method is not too long , I saw a group of friends make complaints about more than 2000 ways in the group , This is a disaster to maintain , Don't make a method too long , Keep the cube simple , Some general logic goes through Filter Or combine AOP To achieve
Sonar There is a way to analyze the complexity of a method , Officially called Cognitive Complexity
A brief introduction , In code if
/switch
/for
/foreach
/try
...catch
/while
Will increase the complexity of the method , If there is a layer of nesting, the complexity will be increased 1, Sonar The default complexity of a method cannot exceed 15
Here are a few simple examples :
The complexity of the following method is 3, There are three if(else) Branch
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
边栏推荐
- An article will take you to understand CSS3 fillet knowledge
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
- A small goal in 2019 to become a blog expert of CSDN
- 【学习】接口测试用例编写和测试关注点
- Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
- ado.net和asp.net的关系
- es创建新的索引库并拷贝旧的索引库 实践亲测有效!
- How to play sortable JS vuedraggable to realize nested drag function of forms
- jenkins安装部署过程简记
猜你喜欢
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
2020-08-14:数据任务的执行引擎用的哪些?
What grammar is it? ]
Swagger 3.0 brushes the screen every day. Does it really smell good?
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
Take you to learn the new methods in Es5
CCR coin frying robot: the boss of bitcoin digital currency, what you have to know
2020-09-09:裸写算法:两个线程轮流打印数字1-100。
Summary of front-end performance optimization that every front-end engineer should understand:
Vue communication and cross component listening state Vue communication
随机推荐
2020-09-03:裸写算法:回形矩阵遍历。
This project allows you to quickly learn about a programming language in a few minutes
Tron smart wallet PHP development kit [zero TRX collection]
How to play sortable JS vuedraggable to realize nested drag function of forms
ES中删除索引的mapping字段时应该考虑的点
打工人好物——磨炼钢铁意志就要这样高效的电脑
ES6 learning notes (2): teach you to play with class inheritance and class objects
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
ado.net和asp.net的关系
Why is quicksort so fast?
What is the purchasing supplier system? Solution of purchasing supplier management platform
2020-08-24:什么是小文件?很多小文件会有什么问题?很多小文件怎么解决?(大数据)
What the hell is fastthreadlocal? The existence of ThreadLocal!!
2020-08-20:GO语言中的协程与Python中的协程的区别?
image operating system windows cannot be used on this platform
Road to simple HTML + JS to achieve the most simple game Tetris
Open source a set of minimalist front and rear end separation project scaffold
JVM memory allocation - xms128m - xmx512m - XX: permsize = 128M - XX: maxpermsize = 512M
C language I blog assignment 03
2020-08-19:TCP是通过什么机制保障可靠性的?