Top 6 Refactoring Patterns to Help You Score 80% in Code Quality

Posted by Ajitesh Kumar / In Code ReviewSoftware 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:

  1. 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.
  2. Long Method: The methods have been found longer due to several reasons such as following:
    1. Several block of code serving unrelated/multiple functionality within the same method. This is primarily due to lack of SRP concepts with the developers.
    2. 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.
  3. 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.
  4. 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.
  5. Vague Method Names: Many a times, method names have been found to look like following which impacts on code read-ability and understand-ability:
    1. Vague names without any meaning
    2. Technical names without any reference to problem domain related names.
Top 6 Refactoring Patterns to Deal with Above Code Smells
 
Following are top 6 refactoring patterns which could help you resolve 80% (remember 80-20 rule) of code quality issues and become a better developer:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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

 
Ajitesh is passionate about various different technologies including programming languages such as Java/JEE, Javascript, PHP, .NET, C/C++, mobile programming languages etc and, computing fundamentals such as application security, cloud computing, API, mobile apps, google glass, big data etc. 

Follow him on Twitter and Google+.
 

Tags: refactoring

 

1 COMMENT

  1. [...] may want to check the refactoring patterns that could help you with tips to avoid these smells and write neat [...]

  2. 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

  3. […] very well about code smells and various different code refactoring patterns which deals with the coding […]

  4. […] very well about code smells and various different code refactoring patterns which deals with the coding […]

  5. […] 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的更多相关文章

  1. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  2. JavaScript资源大全中文版(Awesome最新版)

    Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...

  3. 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 ...

  4. Hive分组取Top N

    Hive在0.11.0版本开始加入了row_number.rank.dense_rank分析函数,可以查询分组排序后的top值   说明: row_number() over ([partition ...

  5. Oracle结合Mybatis实现取表TOP 10

    之前一直使用mysql和informix数据库,查表中前10条数据十分简单: 最原始版本: select top * from student 当然,我们还可以写的复杂一点,比如外加一些查询条件? 比 ...

  6. POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

    Milk Patterns   Description Farmer John has noticed that the quality of milk given by his cows varie ...

  7. taiyi_interview(Introduction To Database Refactoring)

    Introduction To Database Refactoring 原文链接:by Scott W. Ambler:http://www.tdan.com/view-articles/5010/ ...

  8. Top 40 Static Code Analysis Tools

    https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...

  9. 重构(Refactoring)技巧读书笔记(General Refactoring Tips)

    重构(Refactoring)技巧读书笔记 之一 General Refactoring Tips, Part 1 本文简要整理重构方法的读书笔记及个人在做Code Review过程中,对程序代码常用 ...

  10. (转)MapReduce Design Patterns(chapter 4 (part 1))(七)

    Chapter 4. Data Organization Patterns 与前面章节的过滤器相比,本章是关于数据重组.个别记录的价值通常靠分区,分片,排序成倍增加.特别是在分布式系统中,因为这能提高 ...

随机推荐

  1. 6. SpringMVC的视图

    SpringMVC 中的视图是 View 接口,视图的作用渲染数据,将模型 Model 中的数据展示给用户 SpringMVC 视图的种类很多,默认有转发视图和重定向视图 当工程引入 jstl 的依赖 ...

  2. GPT3的局限性:语言多样性、语言理解能力、数据量

    目录 GPT-3 的局限性:语言多样性.语言理解能力.数据量 随着人工智能技术的不断发展,越来越多的语言模型被开发出来,其中最具代表性的就是 GPT-3.然而,尽管 GPT-3 已经在自然语言处理领域 ...

  3. Dlang 与 C 语言交互(二)

    Dlang 与 C 语言交互(二) 随着需求不断增加,发现好像需要更多的东西了.在官网上找不到资料,四处拼凑才有了本文的分享. 上一文(DLang 与 C 语言交互(一) - jeefy - 博客园) ...

  4. python3.8下安装robotframework历险记

    首先非常感谢本文章博主,极大的给与我可以装好的信心(差点要重装python)https://blog.csdn.net/qq_21583077/article/details/107848409?sp ...

  5. go NewTicker 得使用

    转载请注明出处: 在 Go 语言中,time.NewTicker 函数用于创建一个周期性触发的定时器.它会返回一个 time.Ticker 类型的值,该值包含一个通道 C,定时器会每隔一段时间向通道 ...

  6. 学习C++这一篇就够了(提升篇)

    C++中除了面向对象的编程思想外,还有另一种就是泛型编程 主要用到的技术就是模板 模板机制的分类: 函数模板 类模板 函数模板 作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体定制,用虚拟 ...

  7. 【渗透测试】利用Cobalt Strike渗透Windows

    目标 在kali中使用Cobalt Strike对Windows进行渗透 机器环境 kali(服务端):192.168.175.129 win11(攻击机):192.168.175.128 win11 ...

  8. 使用docker安装的tomcat部署activiti-app.war、activiti-admin.war失败(ClassNotFoundException)

    背景 一直以来习惯用docker配置一些本地学习环境,许多教程配置activiti的方式都是通过复制activiti的war包部署在tomcat中,我尝试了一下通过docker的方式遇到了一些不易察觉 ...

  9. Lazy(Func<T>)的异常缓存问题

    Lazy可以提供多线程环境下的安全保障,但是用不好也是会跳到坑里. 我这里使用Lazy<t>(Func<T>)来创建一个Lazy实例,然后在需要的地方访问它的Value属性,它 ...

  10. Linux 脚本:冒泡排序

    #!/bin/bash arr=(3 2 5 4 1) len=${#arr[@]} for i in $(seq 1 $len) ; do index=$(($len - $i - 1)) for ...