It’s Smelly:Smell in Code
By Debojyoti Chakraborty
Learning the code,yes a great fun,It give you the super power to create some great thing.But as Spiderman says:
“With Great Power Comes Great Responsibility”
Now we can sense that if we don’t use this power properly,it can create some problems,yeah…Sound’s really awful…
But it’s true,let’s dive into the topic:)
First we need to know what is the code smell or smelly code:
According to wikipedia in computer programming, a code smell is any characteristic in the source code of a program that possibly indicates a deeper problem. Determining what is and is not a code smell is subjective, and varies by language, developer, and development methodology.
So a code smell is not any compiler error or syntax error.it’s the error in your designed program which can give you real headache in future when you deploy your software in the real world and you want to upgrade it.
Really a great problem,let’s take some look into it,How it is::]
I give some examples of code smells in c++ and it’s not a complete list.So any other smell you spotted in internet or you experienced please comment below.
Long Functions::
All other things being equal, a shorter method is easier to read, easier to understand, and easier to troubleshoot. Refactor long methods into smaller methods if you can.
Duplicate Codes:
“If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.”
By far the most common code smell, Duplicate Code comes in many shapes and sizes. There are the obvious instances where chunks of code are simply copy and pasted around the code base. But there are also more subtle instances too where chunks of code are parameterized to one extent or another. Figuring out that bits of code are duplicates of each other and how to remove that duplication is what separates beginning developers from intermediate and advanced developers.These are only take extra line in your code.
One work ,Two function.
Primitive Obsession
People new to objects usually are reluctant to use small objects for small tasks, such as money classes that combine number and currency, ranges with an upper and a lower, and special strings such as telephone numbers and ZIP codes.
Primitive Obsession is an over-reliance on the built-in simple data types in a language such as integers, floating-point numbers, strings and the like. For example, a string can hold a hexadecimal color code such as C0C0C0. But even a simple Color class can help reduce bugs:
Dead code:
dead code is code that can be removed safely, because it is not invoked anymore at runtime. Not only can it be removed, but it must be removed, because this extra code add unnecessary complexity to the program. Static analysis can find most of dead code in your program (yet not all).
Long parameter list:
If a function has more than three arguments,It is generally create an issue of Long parameter list.
To solve this we can divide the function in other two small sub functions with less arguments which can use later.
Data Clumps
Data items tend to be like children; they enjoy hanging around in groups together. Often you’ll see the same three or four data items together in lots of places: fields in
a couple of classes, parameters in many method signatures. Bunches of data that hang around together really ought to be made into their own object.
A simple example would be a graphics library that takes x and y coordinate parameters all over the place:
These are like party hoppers …Just enjoy……
Conclusion:
So that’s all for today…But there are too much smell around us,So be careful and use your eye to sense it with a cool mind.
Stupid
REFERNCES:
1.https://github.com/lee-dohm/code-smells/
(This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.)