What is Code Smell?

What is Code Smell?

Software development guru Martin Fowler credits his collaborator Kent Beck for coining the term “code smell.” Fowler defines it as such: A code smell is a surface indication that usually corresponds to a deeper problem in the system.

Fowler highlights two important nuances packed into the term “smell.” First, you detect smells effortlessly. The same is true of code smells: They jump out to you immediately.

Second, is that a smell is merely a superficial suggestion that there is bad code. The operative word there is “suggestion.” Here’s an analogy: If you get a whiff of that stinky feet smell, you could be smelling stinky feet. Or you could be smelling parmesan cheese.

Likewise with code. You could see a class that is packed with data but no behaviour. That’s a code smell: Why can’t this class do anything? You investigate further and find that the real problem causing this data-heavy class lies in how you’re dividing data and functionality in the code. Or you could discover that, actually, it makes perfect sense for your application to have this data-heavy class, even if it generally isn’t best practice. But the point is that the code smell led you to investigate and understand your code at deeper levels.

A nice advantage of code smells is that they are detectable by even the most junior members on the team. Because they don’t require extensive knowledge of business logic, or even advanced coding chops, anyone can spot them and contribute to improving the quality of the source code. Novice developers can bring the code smells they identify to more experienced team members and they can explore them together, which creates a fantastic learning opportunity.

Smelly Code Can Become Rotten Code

Smelly code can easily become rotten code. Code rot is the process by which code slowly degrades over time. One cause of code rot is technical debt. A small amount of technical debt is fine – perhaps even inevitable – but it accumulates quickly and can end up seriously hampering your development velocity. An example is a class which accumulates functionality until it becomes a god class.

Types of Code Smell

Duplicate Code

Duplicate code refers to similar code that exists in more than one area.

Contrived Complexity?

When it comes to code, simplicity is critical. Unfortunately, developers often make code complex when it doesn’t have to be. Contrived complexity occurs when you use complex design patterns instead of simpler ones.

Improper Names?

If your variables, classes, and functions lack proper names, this can be an indicator that your code isn’t clean.

Bloaters

Bloaters are classes and methods that increase in size over time – to the point where they become difficult to work with.

Middle Man

Sometimes you end up with a class that does nothing but delegate work to another class – for instance, its method calls are just call the methods of other classes. This is likely the result of previous refactoring that moved functionality elsewhere, leaving the class all but empty. If the class is doing nothing but acting as a “middle man,” it’s just noise in the code that needs to be eliminated.

Long Parameter List

It’s important to cap the number of parameters within a function at three or four. In the event a function has more than three or four parameters, this could indicate that the function needs to be decomposed into multiple smaller functions.

Data Clumps

Data clumps occur when multiple pieces of data go together. One easy way to spot a data clump is when one component doesn’t make sense in isolation but makes sense as a group.

Object-Orientation Abuser

An object-orientation abuser happens when the code doesn’t maximize its object-oriented design.

Dispensables

A dispensable code smell happens when unnecessary code accumulates.

Change Preventers

A change preventer is something that can stop you from easily developing and modifying code.



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

Nishant Goyal的更多文章

社区洞察

其他会员也浏览了