Coding Better Together Series (pt.1) - Code Smells
Eeew! This code stinks!

Coding Better Together Series (pt.1) - Code Smells

One the first series of coding better together we are going to discuss code smells, what they are and why they matter.

What is a Code Smell?

Nobody likes anything that smells. We use a nastier word, we say, “something stinks!”. We all strive for clean code that’s maintainable but sometimes we find ourselves dealing with ugly code. Therefore, a code smell smells?are not bugs or errors. Instead, these are absolute?violations?of the?fundamentals of developing?software?that decrease the quality of code.

When we start coding a project, we have to observe the first rule of three. But wait, what is the Rule of Three for refactoring?

The Rule of Three for refactoring are:

  1. When you’re doing something for the first time, just get it done.
  2. When you’re doing something similar for the second time, cringe at having to repeat but do the same thing anyway.
  3. When you’re doing something for the third time, start refactoring.

No alt text provided for this image

Therefore, if we start a project, as developers, we just code away. We use the path of least resistance even if it includes having one big class/function with everything inside.

Problems start arising when we need to maintain the software, meaning you need to update your code based on new and/or changing requirements. Then we enter the second rule of three. We start noticing that some parts of our code look ugly, they stink! But we can live with it so we leave it alone. But more user requirements come in and changing the code becomes a pain in the a$$ therefore, we experience the need to refactor the code, enter rule three.

Categories of Code Smells

Being a software developer, whether senior or junior, avoiding code smells is a never-ending job. A major part of our work is to provide quality, readable, maintainable and most of all clean code. One of the genius software engineers in the world is lucky to have named Kent Beck the creator of extreme programming a.k.a XP, who coined the term Code Smells in the late 1990s.

Bloaters

These are parts of our code, methods and even classes that have increased in size and prevent us from working because of the high mental capacity they bestow on us developers. They include:

  • Long Method
  • Primitive Obsession
  • Data Clumps
  • Large Class
  • Long Parameter List

Object-Orientation Abusers

These are smells that arise from misuse of object-oriented programming principles. They include:

  • Alternative Classes with Different Interfaces
  • Refused Bequest
  • Switch Statements (I know right!)
  • Temporary Field

Change Preventers

In your code when you find yourself in need of changing some part of your code but can’t quite do it because it means changing many other places too. Then you have experienced code smells in the change preventers category. They include:

  • Divergent Change
  • Parallel Inheritance Hierarchies
  • Shotgun Surgery

Dispensable(s)

The most common code smell in this category is dead code. These are code smells where the piece of code, method or class is no longer needed. They include:

  • Dead Code
  • Data Class
  • Duplicate Code
  • Comments
  • Lazy Class
  • Speculative Generality – (I suffer from this sometimes)

Couplers

No alt text provided for this image

Sometimes two entities like two classes may end up tightly coupled together or some parts of our code get excessively delegated to tasks or functionality outside its scope. These types of situations are normally under couplers category of code smells

  • Feature Envy
  • Inappropriate Intimacy
  • Incomplete Library Class
  • Message Chains
  • Middle Man

Why should I care for code smells?

In some situation you are a lone developer and you have a grasp of how things work. The problem is no man is an island. Therefore, you will require some help at some point. This will lead to team forming. If you don’t take care of the ugly parts of your code then this will be a problem since the ones who get to join you will have no idea what is going on. Even worse they will have a difficult time adding or changing code without the fear of breaking things. Following the best coding standards will increase the amount of time it takes to release software but software maintainability is more important than speed. Therefore, knowing and understanding how your code might smell will help make the working place a refreshing place to work not only for you but also for your teammates.

Conclusion

On the next episode of this series, we shall examine the first code smell – Bloaters ~ Long Methods. Stay tuned and thank you for your time.

要查看或添加评论,请登录

John W.的更多文章

社区洞察

其他会员也浏览了