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 of you. But the image nicely represents how to find if your code really smells. The code smells listed in this article would certainly lead to increase in WTF count ![]()
Representing Code Smells in form of WTF count :-)
For newbie developers, it has always remained a challenge to write code of great quality from day one. One of the primary concerns they have been found to have is following:
What kind of code could be termed as high quality code?
There are several characteristics of good quality code. However, a good starting point would be to write code which could be easily maintainable and usable (readable and understandable). Maintainability and Usability are two very important characteristic of software quality as per ISO 25000 as well. Two important features of a highly maintainable code are testability and reusability. Thus, the three key code quality that newbies could keep in mind are testability, reusability and usability.
Top 5 Code Smells Which are Easier to Identify & Avoid
If only newbies could watch following 5 areas (code smells) and avoid them, they would mostly end up with the code conforming to the requirements of testability, reusability and usability:
- Long Classes: It would be good to keep a watch on number of lines of code (LOC) in the class. If it crosses more than 300 or so, you may want to check on the fact if the methods of the class are related and serving just one functionality our doing just one thing. This is in line with the famous SOLID principle namely Single Responsibility Principle. Another area to look at is if individual methods are getting longer enough whose detail is describe in next point related with long method. Having classes serving just one functionality is easier to change as there could be only one set of related reasons for which the class would be needed to change. Thus, these classes becomes greatly maintainable meaning testable and reusable. These classes can also be termed as cohesive classes.
- Long Methods: If the LOC in a method increases more than 50-60, it is time to watch out and make sure that method is just doing one thing. Most of the times, methods get longer because of following two reasons:
- Methods consist of block of codes performing multiple functionality which can also be classified as violation of “single responsibility principle”. In this case, one could consider extracting block of codes performing different functionality into different methods.
- Methods consist of long conditional statements consisting of if-else statements which can again be extracted into different methods.
- Long Conditionals Expressions: At times, methods get longer due to large number of conditional statements in the same method. This can also be explained in terms of McCabe Cyclomatic Complexity which can be calculated as number of conditional statement + 1. These conditional statements looks like if-else, while, for, switch etc. If the cyclomatic complexity of a method becomes larger than 10 or so, it is time to watch out and see if these conditional expressions could be extracted into separate method. Long conditional expressions greatly reduces code usability that is code readability and understand-ability. More importantly, if you a unit testing freak, more the conditional statements in your method, more it becomes difficult to achieve a higher code coverage.
- Long Parameter List: Newbies developers tend to create method with long parameter list that creates issue at later stage when many other methods have called these methods and there arises a need to add another parameter to the existing list. Once you see that parameter list of a method increases further than 4-5 parameters, it is time to examine whether these parameters are related and could be created as a parameter object and passed to methods as objects. This makes it easy to add further parameters to parameter object without the need to change the method signature and the code becomes greatly maintainable.
- Vague Classes & Method Names: This one is very simple to identify. If you can not get a good name to name your classes and methods, be sure that you may be missing on business requirements which is supposed to be served by the method or class. Alternatively, for other developers, if the method and class names becomes difficult to understand, it is time to think through and come up with the name that could help developers to understand the functionality that is supposed to be served the method or the class.
You may want to check the refactoring patterns that could help you with tips to avoid these smells and write neat code.
Ajitesh Kumar
Follow him on Twitter and Google+.
Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid的更多相关文章
- How to code like a pro in 2022 and avoid If-Else
在浏览文章的时候发现了一篇叙述有关if-else语句的文章,这篇文章作者是Thai Tran,他原文是用英语写的,然后看着文章浅显易懂,便尝试翻译成汉语.如有不妥还望指出. 原文链接:https:// ...
- Code Review Checklist and Guidelines for C# Developers
Checklist1. Make sure that there shouldn't be any project warnings.2. It will be much better if Code ...
- Top 40 Static Code Analysis Tools
https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...
- Top 10 Mistakes Java Developers Make--reference
This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ...
- Learn golang: Top 30 Go Tutorials for Programmers Of All Levels
https://stackify.com/learn-go-tutorials/ What is Go Programming Language? Go, developed by Google in ...
- Java – Top 5 Exception Handling Coding Practices to Avoid
This article represents top 5 coding practices related with Java exception handling that you may wan ...
- Career Planning:Developers Best Practices Tutorial
This small tutorial is based on my past 16+ years of experience in software development industry. I ...
- 读《Top benefits of continuous integration》有感
看到一片文章<Top benefits of continuous integration>,这张图画的很棒.将整个CI流程各阶段,列举出来了. 作者在文章里面介绍了CI和TDD,以及采用 ...
- Top 10 Uses For A Message Queue
We’ve been working with, building, and evangelising message queues for the last year, and it’s no se ...
- TOP 10 ONLINE COMPILER
Top 10 Online Compilers +1338 Tweet Share106 Share Pin 444 Shares Online compilers are one type of t ...
随机推荐
- 【python基础】类-类属性
在初始类中,我们介绍了如何访问类属性,除了访问类属性外还有其他操作类属性的情况,我们将在这里做详细介绍: 1.给类属性指定默认值 类中的每个属性都必须有初始值,哪怕这个值是0或者空字符串.在有些情况下 ...
- 万字长文 | Hadoop 上云: 存算分离架构设计与迁移实践
一面数据原有的技术架构是在线下机房中使用 CDH 构建的大数据集群.自公司成立以来,每年都保持着高速增长,业务的增长带来了数据量的剧增. 在过去几年中,我们按照每 1 到 2 年的规划扩容硬件,但往往 ...
- CRM系统化整合从N-1做减法实践
1 背景 京销易系统已经接入大网.KA以及云仓三个条线商机,每个条线商机规则差异比较大,当前现状是独立实现三套系统分别做支撑. 2 目标 2022年下半年CRM目标是完成9个新条线业务接入,完成销售过 ...
- Ubutnu 20.04 安装和使用单机版hadoop 3.2 [转载]
按照此文档操作,可以一次部署成功:Ubutnu 20.04 安装和使用单机版hadoop 3.2 部署之后,提交测试任务报资源问题.原因是yarn还需要配置,如下: $ cat yarn-site.x ...
- 深入Scikit-learn:掌握Python最强大的机器学习库
本篇博客详细介绍了Python机器学习库Scikit-learn的使用方法和主要特性.内容涵盖了如何安装和配置Scikit-learn,Scikit-learn的主要特性,如何进行数据预处理,如何使用 ...
- 助力618-Y的混沌实践之路
一.写在前面 1.混沌是什么? 混沌工程(Chaos Engineering)的概念由 Netflix 在 2010 年提出,通过主动向系统中引入异常状态,并根据系统在各种压力下的行为表现确定优化策略 ...
- 基于Linux的三种防火墙(IPtables、Firewall、UFW)
学而不思则罔,思而不学则殆. 导航 IPtables Firewall UFW 对比总结 IPtables部分 1.IPtables 四表五链. 四表:filter.nat.raw.mangle. 五 ...
- IDApython练习1-脚本去花
IDApython练习1-脚本去花 这里主要是练习IDApython脚本去花 1 这里 jz跳转条件是zf=1, jnz跳转条件是zf=0, 但是zf就2种可能,所以无论如何都会跳转到loc_411D ...
- IDA常用的插件
IDA常用的插件 FindCrypto https://github.com/polymorf/findcrypt-yara 算法识别 缺点:对于魔改的地方难以识别,比如对aes的s盒进行加密,运行时 ...
- 通过WinSW部署JAR包为windows服务
通过WinSW部署JAR包为windows服务 背景 使用 Java 编写了一些有用的工具,因为不方便部署到服务器上,所以需要把 Java 生成的 jar 包在本地 Windows 上部署. 查阅了几 ...