当前位置:网站首页>代码重构之法——方法重构分析
代码重构之法——方法重构分析
2020-11-06 21:35:00 【程序猿欧文】
代码重构之法——方法重构分析
Intro
想要写出比较优秀的代码,需要时刻警惕代码中的坏味道,今天想写一篇文章介绍一下如何分析你的方法是不是需要考虑重构
一个方法通常有三个部分组成,输入(Input),输出(Output),方法体(Method Body),我们就从这三个方面来分析一个方法是否该考虑重构
Input
方法输入也就是方法的参数,通常来说一个方法的参数基本可以控制在7个以内(仅作参考,可以自己衡量,SonarQube 默认方法最多七个参数),如果你的方法参数过多的话,可能就需要考虑重构一个方法参数了,通常的做法是封装一个独立的 model,参数作为 model 的属性。
举一个常见的例子,比如一个新闻列表的API,起初可能很简单,就只需要一个 lastId,一个 count 两个参数,但是随着业务需求的增加,可能会增加很多别的参数,比如前端提供一个 keyword 进行全文检索,提供一个 sortBy 进行排序,根据新闻标题匹配,作者名称匹配,分类匹配,根据发布时间筛选等等,最后可能会导致这个方法的参数有很多
通常我会新增一个 XxxRequest 的 model,然后方法参数替换成这个 model,然后指定 [FromQuery] 就可以了,可以对比一个修改前后的差异,是不是后面的方式更清爽一些呢
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 就是方法的返回值,尽可能返回具体的类型,尽可能避免使用 Tuple 等类型,方法的返回值应该具有明确的意义
使用具体的 Model 代替 Tuple 返回值,尤其是一些 public 的,要被外部访问的方法更应该返回具体的类型,虽然 C# 7.2 开始支持了 named tuple,会比之前友好很多,支持给 tuple 指定名称,但是这只是编译器级别的,实际还是 Item1,Item2 ...,还是比较推荐使用具体的 model,更加明了
Body
通常一个方法不要太长,曾经在群里看到群友吐槽一个方法两千多行,这样的方法维护起来简直就是灾难,不要让一个方法太长,保持方体体的简单,一些通用的逻辑通过 Filter 或结合 AOP 来实现
Sonar 有一个分析方法复杂度的一个方法,官方称之为 Cognitive Complexity
简单介绍一下,代码里的 if/switch/for/foreach/try...catch/while 都会增加方法的复杂度,出现一层嵌套则复杂度再加1, Sonar 默认的一个方法的复杂度不能超过 15
来几个简单的示例:
下面这个方法的复杂度是 3,有三个 if(else) 分支
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4554128
边栏推荐
- Chainlink brings us election results into blockchain everipedia
- Three Python tips for reading, creating and running multiple files
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- Recommendation system based on deep learning
- Basic principle and application of iptables
- python100例項
- From overseas to China, rancher wants to do research on container cloud market
- 【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
- Basic usage of GDB debugging
- Building a new generation cloud native data lake with iceberg on kubernetes
猜你喜欢
随机推荐
C語言I部落格作業03
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Introduction to the structure of PDF417 bar code system
【ElasticSearch搜索引擎】
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
How to customize sorting for pandas dataframe
Free patent download tutorial (HowNet, Espacenet)
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
What are PLC Analog input and digital input
华为Mate 40 系列搭载HMS有什么亮点?
Discussion on the development practice of aspnetcore, a cross platform framework
ERD-ONLINE 免费在线数据库建模工具
这个项目可以让你在几分钟快速了解某个编程语言
有了这个神器,快速告别垃圾短信邮件
DRF JWT authentication module and self customization
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
Use modelarts quickly, zero base white can also play AI!
用一个例子理解JS函数的底层处理机制
Shh! Is this really good for asynchronous events?
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
