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

Powered by Starbox

Related posts:

  1. Refactoring 3000 Lines of Code
  2. Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid
  3. PHP Code Smells and Best Practices
  4. Stay Single and Get Admired for Your Code

Tags: refactoring

1 COMMENT

  1. Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid | Software Developer Cravings

    FEBRUARY 1, 2014 AT 10:15 AM

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

    REPLY

  2. Eyal

    FEBRUARY 2, 2014 AT 2:53 AM

    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

    REPLY

  3. The 7 Habits of Highly Effective Developers - Software Developer Cravings

    FEBRUARY 11, 2014 AT 11:03 AM

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

    REPLY

  4. The 7 Habits of Highly Effective Developers

    FEBRUARY 11, 2014 AT 2:11 PM

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

    REPLY

  5. 6 个重构方法可帮你提升 80% 的代码质量 – ISURE

    JUNE 26, 2014 AT 2:05 PM

    […] Kumar是位涉猎广泛的软件工程师,对很多技术领域都有非常高的热情,如Java/JEE、PHP、.NET、C/C++等程序设计语言、移动编程语言、应用安全、云计算、API、移动应用、Google Glass、大数据等等,其Twitter帐号是@eajitesh。近日,Kumar撰写了一篇文章,谈到了常见的代码坏味道以及改善代码质量的6种重构模式,并对每种重构模式的使用场景进行了详尽的论述与讨论。

时间: 2024-10-19 07:09:37

Top 6 Refactoring Patterns to Help You Score 80% in Code Quality的相关文章

refactoring Patterns:第二部分

阿里妹导读:以深度学习为代表的人工智能在图像.语音和NLP领域带来了突破性的进展,在信息检索和个性化领域近几年也有不少公开文献,比如wide& deep实现了深度模型和浅层模型的结合,dssm用于计算语义相关性,deepfm增加了特征组合的能力,deep CF用深度学习实现协同过滤,rnn recommender 采用行为序列预估实现个性化推荐等. 工业级的信息检索或个性化系统是一个复杂的系统工程,深度学习的工业级应用需要具备三个条件:强大的系统计算能力,优秀的模型设计能力和合适的应用场景.今天

常用的6种代码重构方法帮你提升80%的代码质量

在过去做了不少代码走读,发现了一些代码质量上比较普遍的问题,以下是其中的前五名: 臃肿的类: 类之所以会臃肿,是因为开发者缺乏对最基本的编码原则,即“单一职责原则”(SRP)的理解.这些类往往会变得很臃肿,是由于不同的且在功能上缺少关联的方法都放在了相同的类里面. 长方法: 方法之所以会变得很长主要是有以下几个原因: 许多没有关联性的.功能复杂的模块的代码都放在相同的方法内.这主要是开发者缺乏SRP的概念. 多种条件都放在同一个方法内,这在长方法内经常会发生的.这是由于缺乏McCabe代码复杂度

6 个重构方法可帮你提升 80% 的代码质量

英文原文:Top 6 Refactoring Patterns to Help You Score 80% in Code Quality 在过去做了不少代码走读,发现了一些代码质量上比较普遍的问题,以下是其中的前五名: 臃肿的类: 类之所以会臃肿,是因为开发者缺乏对最基本的编码原则,即“单一职责原则”(SRP)的理解.这些类往往会变得很臃肿,是由于不同的且在功能上缺少关联的方法都放在了相同的类里面. 长方法: 方法之所以会变得很长主要是有以下几个原因: 许多没有关联性的.功能复杂的模块的代码都

6 个重构方法可帮你提升 80% 的代码质量(转)

英文原文:Top 6 Refactoring Patterns to Help You Score 80% in Code Quality 在过去做了不少代码走读,发现了一些代码质量上比较普遍的问题,以下是其中的前五名: 臃肿的类: 类之所以会臃肿,是因为开发者缺乏对最基本的编码原则,即“单一职责原则”(SRP)的理解.这些类往往会变得很臃肿,是由于不同的且在功能上缺少关联的方法都放在了相同的类里面. 长方法: 方法之所以会变得很长主要是有以下几个原因: 许多没有关联性的.功能复杂的模块的代码都

(转)6 个重构方法可帮你提升 80% 的代码质量

英文原文:Top 6 Refactoring Patterns to Help You Score 80% in Code Quality 在过去做了不少代码走读,发现了一些代码质量上比较普遍的问题,以下是其中的前五名: 臃肿的类: 类之所以会臃肿,是因为开发者缺乏对最基本的编码原则,即“单一职责原则”(SRP)的理解.这些类往往会变得很臃肿,是由于不同的且在功能上缺少关联的方法都放在了相同的类里面. 长方法: 方法之所以会变得很长主要是有以下几个原因: 许多没有关联性的.功能复杂的模块的代码都

Awesome Python

Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Environment Management Package Management Package Repositories Distribution Build Tools Interactive Interpreter Fi

变量运算符

--表一:学生表 student--学号:code int (主键)从1开始--姓名:name varchar(50)--性别:sex char(10)--班级:banji char(10)--语文教师编号:yujiao int --数学教师编号:yujiao int --英语教师编号:yujiao int --表二:教师表 teacher--教师编号:code int (主键) 从1001开始--负责课程:lesson char(10)(语文.数学.英语)--年龄:age int--生日:bi

一道SQL的面试题之联想

一道SQL的面试题之联想 本人工作在一家小型的民营企业,主要从事业务系统的日常维护,二次开发,菜鸟一枚.周五经理准备面试两个开发人员,据简历,都还比较不错,让经理产生了想法,于是准备了一套面试题目,给我们亮了一道SQL题目,非他之手,据出此题者说,如果面试者只能写出一种方法,基本可以pass.请读者仔细看题: 题目:如下表,用一条select语句求出所有课程在80分(含80分)以上的学生姓名,请写出所有可行方案.(注意:表名为sc,字段为name,kc,score) 这道题目并不陌生吧,相信大家

Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid

Posted by Ajitesh Kumar / In Freshers, Software Quality / February 1, 2014 Following is one very popular image which has been used to represent time and again for representing code smells. I am doing it again. Apologies if this one is repeat for some