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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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

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

Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid的更多相关文章

  1. How to code like a pro in 2022 and avoid If-Else

    在浏览文章的时候发现了一篇叙述有关if-else语句的文章,这篇文章作者是Thai Tran,他原文是用英语写的,然后看着文章浅显易懂,便尝试翻译成汉语.如有不妥还望指出. 原文链接:https:// ...

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

  3. Top 40 Static Code Analysis Tools

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

  4. Top 10 Mistakes Java Developers Make--reference

    This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ...

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

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

  7. Career Planning:Developers Best Practices Tutorial

    This small tutorial is based on my past 16+ years of experience in software development industry. I ...

  8. 读《Top benefits of continuous integration》有感

    看到一片文章<Top benefits of continuous integration>,这张图画的很棒.将整个CI流程各阶段,列举出来了. 作者在文章里面介绍了CI和TDD,以及采用 ...

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

  10. TOP 10 ONLINE COMPILER

    Top 10 Online Compilers +1338 Tweet Share106 Share Pin 444 Shares Online compilers are one type of t ...

随机推荐

  1. 发布一个Visual Studio 2022 插件,可以自动完成构造函数依赖注入代码

    赖注入(DI)在开发中既是常见的也是必需的技术.它帮助我们优化了代码结构,使得应用更加灵活.易于扩展,同时也降低了各个模块之间的耦合度,更容易进行单元测试,提高了编码效率和质量.不过,手动注入依赖项也 ...

  2. Taurus .Net Core 微服务开源框架:Admin 插件【1】 - 微服务节点管理

    前言: 最近发现 NetCore 的文章有点少,特来补几篇. 上一篇:Taurus.mvc .Net Core 微服务开源框架发布V3.1.7:让分布式应用更高效. 自上篇之后,期间更新了4个小版本, ...

  3. Linux系统运维之zabbix配置tomcat监控

    一.介绍 半年前安装的zabbix监控,当时配合异地的测试人员给A项目做压力测试,主要监控项目部署的几台服务器的内存.CPU信息,以及后来网络I/O等,也没考虑JVM:最近闲下来,想完善下监控,故留此 ...

  4. async-await Rust: 200 多行代码实现一个极简 runtime

    What I cannot create, I do not understand Rust 中的 runtime 到底是咋回事, 为了彻底搞懂它, 我在尽量不借助第三方 crate 的情况下实现了一 ...

  5. 缕析条分Scroll属性

    最近有项目需要使用js原生开发滑动组件,频繁要用到dom元素的各种属性,其中以各种类型的height和top属性居多,名字相近,含义也很容易搞混.因此特地总结归纳了一下常用的知识点,在文末我们来挑战实 ...

  6. 伸展树(Splay)详解

    引入 在一条链中,二叉查找树的时间复杂度就会退化成 \(O(n)\),这时我们就需要平衡树来解决这个问题. \(Splay\)(伸展树)是平衡树的一种,它的每一步插入.查找和删除的平摊时间都是 \(O ...

  7. 用 Hugging Face 推理端点部署 LLM

    开源的 LLM,如 Falcon.(Open-)LLaMA.X-Gen.StarCoder 或 RedPajama,近几个月来取得了长足的进展,能够在某些用例中与闭源模型如 ChatGPT 或 GPT ...

  8. js闭包的一些笔记

    闭包 闭包是一个可以访问外部作用域的内部函数,即使这个外部作用域已经执行结束 作用域 作用域决定这个变量的生命周期及其可见性.当我们创建一个函数,就会生成一个新的作用域. 通过var创建的变量只有函数 ...

  9. 【微信小程序的开发】初步认识

    目录 项目结构 页面组成 json配置文件 ​ app.json ​ project.config.json ​ sitemap.json ​ 每个页面的json ​ 实例 wxml ​ 标签名称 ​ ...

  10. Linux中对管道命令中的任意子命令进行返回码校验

    ~~ linux return code with pipeline~~ ~~ linux 管道命令中的返回码~~ BASH SHELL中,通常使用 $? 来获取上一条命令的返回码. Shell Sc ...