Top 6 Refactoring Patterns to Help You Score 80% in Code Quality
Top 6 Refactoring Patterns to Help You Score 80% in Code Quality
Posted by Ajitesh Kumar / In Code Review, Software Quality / January 31, 2014
Have done several code reviews in past and found following top 5 code smells common across most of these code having code quality issues:
- Large Class: The classes were found larger enough due to lack of developers’ understanding on one of the primary coding principles which is “Single Responsibility Principle” (SRP). These classes use to get larger due to various methods serving unrelated functionality in the same class.
- Long Method: The methods have been found longer due to several reasons such as following:
- Several block of code serving unrelated/multiple functionality within the same method. This is primarily due to lack of SRP concepts with the developers.
- Several conditionals within the same method. This is found very often within the method which are long enough. This may be attributed to the lack of knowledge on McCabe code complexity vis-a-vis SRP concepts.
- Several Method Parameters : Then, there are several instances I came across where it was seen that method is passing several parameters to each other to interact/call each other. And, a change to one of the parameters in the parameter list use to change several method signature.
- Literal Constants Used Everywhere: Several times, it is seen that developers (primarily newbies) use the literal constants (mostly numbers) with definite meanings using them as it is, without assigning a proper named constant variable to it. This degrades the code read-ability and understand-ability.
- Vague Method Names: Many a times, method names have been found to look like following which impacts on code read-ability and understand-ability:
- Vague names without any meaning
- Technical names without any reference to problem domain related names.
- Extract Class/Move Method: As mentioned above, code smell such as large class serving functionality that should be done by two or more classes should be split/extracted into appropriate number of classes. In these new classes, the appropriate fields and methods from the old classes should be moved. Additionally, the classes are also found to be long at times because of presence of methods which serves functionality which are used by other class and tends to be the member method of that class. Such methods should also be moved to such type of appropriate class.
- Extract Method: Code smell such as long method as mentioned above could be dealt by extracting code from old method to one or more new methods.
- Decompose Conditional: many a times, method found to be long is seen to consist of several conditional statements (if-else). These conditionals should be extracted and moved into separate methods. This does increase the code read-ability & understand-ability to a great extent.
- Introduce Parameter Object/Preserve Whole Object: Several parameters passed to methods has been one another common observations I have come across while doing code review. The problem has been primarily related to change in code if there has been a need to add or remove one of the method parameters from the involved methods. In this scenario, it is suggested to group related parameters as object (Introduce Parameter Object) and let methods pass these objects rather than each of the parameters.
- Replace Magic Number with Symbolic Constants: For literal constants that have meanings and that are used everywhere, should be assigned a named constant variable. This enhances the code read-ability and understand-ability to a great extent.
- Rename Method: As mentioned above under the code smells, the vague method names impacts the usability of the code. These vague names should rather be given meaningful names which may relate to business domain related terminologies and help developer understand the code in the business context. This is quite a skill and requires developers to collaborate with business analysts to properly understand the business requirements that is met with the code. Interestingly, this refactoring pattern looks pretty simple to understand, but however, gets ignored quite often by many developers given the fact that its mention is made in IDEs such as Eclipse under refactor menu link.
Ajitesh Kumar
Follow him on Twitter and Google+.
Tags: refactoring
1 COMMENT
[...] may want to check the refactoring patterns that could help you with tips to avoid these smells and write neat [...]
Hi,
Nicely written blog !
I just wrote a blog about the Single Responsibility Principle.
I talk there on some of the stuff you mention.
Would appreciate your comments
http://eyalgo.com/2014/02/01/the-single-responsibility-principle/
Thanks
[…] very well about code smells and various different code refactoring patterns which deals with the coding […]
[…] very well about code smells and various different code refactoring patterns which deals with the coding […]
[…] Kumar是位涉猎广泛的软件工程师,对很多技术领域都有非常高的热情,如Java/JEE、PHP、.NET、C/C++等程序设计语言、移动编程语言、应用安全、云计算、API、移动应用、Google Glass、大数据等等,其Twitter帐号是@eajitesh。近日,Kumar撰写了一篇文章,谈到了常见的代码坏味道以及改善代码质量的6种重构模式,并对每种重构模式的使用场景进行了详尽的论述与讨论。
Top 6 Refactoring Patterns to Help You Score 80% in Code Quality的更多相关文章
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- JavaScript资源大全中文版(Awesome最新版)
Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...
- Game Development Patterns and Best Practices (John P. Doran / Matt Casanova 著)
https://github.com/PacktPublishing/Game-Development-Patterns-and-Best-Practices https://github.com/m ...
- Hive分组取Top N
Hive在0.11.0版本开始加入了row_number.rank.dense_rank分析函数,可以查询分组排序后的top值 说明: row_number() over ([partition ...
- Oracle结合Mybatis实现取表TOP 10
之前一直使用mysql和informix数据库,查表中前10条数据十分简单: 最原始版本: select top * from student 当然,我们还可以写的复杂一点,比如外加一些查询条件? 比 ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
- taiyi_interview(Introduction To Database Refactoring)
Introduction To Database Refactoring 原文链接:by Scott W. Ambler:http://www.tdan.com/view-articles/5010/ ...
- Top 40 Static Code Analysis Tools
https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...
- 重构(Refactoring)技巧读书笔记(General Refactoring Tips)
重构(Refactoring)技巧读书笔记 之一 General Refactoring Tips, Part 1 本文简要整理重构方法的读书笔记及个人在做Code Review过程中,对程序代码常用 ...
- (转)MapReduce Design Patterns(chapter 4 (part 1))(七)
Chapter 4. Data Organization Patterns 与前面章节的过滤器相比,本章是关于数据重组.个别记录的价值通常靠分区,分片,排序成倍增加.特别是在分布式系统中,因为这能提高 ...
随机推荐
- CatBoost的分布式训练与调优:解决大规模数据集问题
目录 <CatBoost 的分布式训练与调优:解决大规模数据集问题> 引言 随着深度学习的兴起,大规模数据集的存储和处理成为一个重要的技术挑战.由于数据集的规模巨大,传统的分布式训练方法已 ...
- 大语言模型(LLM)在文本分类、语言生成和文本摘要中的应用
目录 大语言模型(LLM)在文本分类.语言生成和文本摘要中的应用 引言 文本分类.语言生成和文本摘要是人工智能领域中的重要任务,涉及到自然语言处理.机器学习和深度学习等领域.本文将介绍大语言模型(LL ...
- go web学习(四)
跟着b站https://space.bilibili.com/361469957 杨旭老师学习做的笔记 中间件 什么是中间件 请求----> 中间件 ----> Handler 响应 &l ...
- MAUI Blazor Android 输入框软键盘遮挡问题
前言 最近才发现MAUI Blazor Android存在输入框软键盘遮挡这个问题,搜索了一番,原来这是安卓webview一个由来已久的问题,还好有大佬提出了解决方案 AndroidBug5497Wo ...
- Redis从入门到放弃(1):安装配置
1. 介绍 Redis是一个高性能的开源key-value数据库.它被广泛应用于缓存.会话存储.实时分析.消息队列等场景.Redis具有以下三个主要特点: 数据持久化:Redis支持将内存中的数据保存 ...
- GitLab 无仓库 中了勒索病毒
坑爹的记录一下,并没有解决 Gitlab 昨天(2021-11-29)打开之后看不到项目了,下面这个吊样子 最后发现中病毒了,一堆的这个吊毛文件,复制一个打开看了一下 你别说这个黑客网页写的还不错,这 ...
- Cilium 系列-7-Cilium 的 NodePort 实现从 SNAT 改为 DSR
系列文章 Cilium 系列文章 前言 将 Kubernetes 的 CNI 从其他组件切换为 Cilium, 已经可以有效地提升网络的性能.但是通过对 Cilium 不同模式的切换/功能的启用,可以 ...
- 拖拽宫格vue-grid-layout详细应用及案例
目录 1.前言 2.安装 3.属性 4.事件 5.占位符样式修改 6.案例 1.前言 vue-grid-layout是一个适用于vue的拖拽栅格布局库,功能齐全,适用于拖拽+高度/宽度自由调节的布局需 ...
- [grpc]双向tls加密认证
前言 假设gRPC服务端的主机名为qw.er.com,需要为gRPC服务端和客户端之间的通信配置tls双向认证加密. 生成证书 生成ca根证书.生成过程会要求填写密码.CN.ON.OU等信息,记住密码 ...
- 一文详解TextBrewer
本文分享自华为云社区<TextBrewer:融合并改进了NLP和CV中的多种知识蒸馏技术.提供便捷快速的知识蒸馏框架.提升模型的推理速度,减少内存占用>,作者:汀丶. TextBrewer ...