HusseinCheayto
Code that smells

10 CODING MISTAKES THAT MAKE YOUR CODE SMELL

I've created this list by walking through several different programs, coding books (like "The Pragmatic Programmer" by Andy Hunt & Dave Thomas and "Clean Code" by Rober C. Martin) and refactoring them. As I made each change, I always think why I made that change. The result is a very long list of things that smell bad to me when I read code. In this article, I have mentioned the top 10 coding mistakes that I've encountered when reading code.

1- Redundant Comment

A comment is redundant if it describes something that describes itself.
For example:
i++; // increment i

2- Poorly Written Comment

Take your time writing comments and make sure to take the time to make it is the best comment you can write. Use correct grammar and punctuation and be brief.
For example:

Public void DisplayTutorial() //when used, the tutorial window will be opened to show the tutorials

Correction:

Public void DisplayTutorial() //Displays window

Related Article: Top 5 Web Developers You Should Follow to Succeed Without a Degree.

3- Commented-Out Code

How many times have you seen hundreds of code commented?? Who knows how old it is? Who knows whether or not it’s meaningful? Yet no one will delete it because everyone assumes someone else needs it or has plans for it.
That code sits there and rots, getting smelly more and more with every passing day. Solution??? Very simple. just DELETE IT!! Don’t worry, if anyone really needs it, he/she can go back and check out a previous version.

4- Too Many Arguments

Functions should have a small number of arguments. No argument is best, followed by one, two, and three. More than three is very questionable and should be avoided with prejudice.

Related Article: 10 People to Follow to Land a Job in Game Development Without a Degree

5- One Function Multiple Tasks

Ideally, every function should serve only ONE task.

For instance:

public void Calculate(float a,float b,float c)
{
if(Add)
{
a=b+c;
}else if (Substract)
{
a=b-c;
}else if(multiply)
{
a=b*c;
}
}

Correction:
public void Add(float a, float b,float c)
{
a=b+c
}

public void Subtract(float a, float b,float c)
{
a=b-c
}

public void Multiply(float a, float b,float c)
{
a=b*c
}

6- Dead Function

Methods that are never called should be discarded. Keeping dead code around is wasteful. Don’t be afraid to delete the function. Remember, your source code control system still remembers it.

Related Article: Get Rich While Sleeping

7- Inconsistency

Consistency, when applied as it should be, can make code much easier to read and modify.
Inconsistency is the habit to do something a certain way. Once done, do all similar things in the same way. Be careful with the conventions you choose, and once chosen, be careful to continue to follow them.
For example:
Using variable names as 'Quit' and 'Exit' instead of sticking to either 'Quit' or 'Exit'.

8- Duplication


Duplication is a very serious problem. Almost every author who writes about software design mentions this rule. Dave Thomas and Andy Hunt called it the DRY3 principle (Don’t Repeat Yourself). Kent Beck made it one of the core principles of Extreme Programming and called it: “Once, and only once.”
It's simple, find and eliminate duplication wherever you can.
For example:
Duplications appear when using the switch/case or if/else chain that appears again and again in various modules, always testing for the same set of conditions.
Correction
Think about using polymorphism.

Related Article: HOW TO BECOME SUCCESSFUL- EPISODE 3- BRYAN GUERRA

9- Misplaced Responsibility


One of the most important decisions a software developer can make is where to put code.
For example:
where should the PI constant go? Should it be in the Math class? Perhaps it belongs in the Trigonometry class? Or maybe in the Circle class?

Correction
Code should be placed where a reader would naturally expect it to be. The PI constant should go where the trig functions are declared. We’ll put it in a function that’s convenient for us, but not necessarily intuitive to the reader.

10- Function and Variable Names

Function names should say what they do. Always choose Descriptive Names and take your time picking a name, it is worth it. You will spend sometime thinking, but it will definitely save you time later, when you read your code after a month or a year.
Look at this code:
Date newDate = date.add(5);
Would you expect this to add five days to the date? Or is it weeks, or hours?
You can’t tell from the call what the function does.

Correction:
If the function adds five days to the date and changes the date, then it should be called IncreaseByDays or AddDays.

Want to become successful? Join my email list and you will receive articles, freebies and free 30 minutes 1 on 1 online coaching session from our talented and experienced developers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Rating*