10 CODING MISTAKES THAT MAKE YOUR CODE SMELL
Tags: programming, coding, advice
Published: 2019-08-18 16:24:15

---
title: 10 CODING MISTAKES THAT MAKE YOUR CODE SMELL
published: true
description:This is a list of tips that every programmer should avoid if one wants his/her code to stay clean. 
tags: #programming, #coding, #Advice, #
---
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 <i>why</i> 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.

<h2><b>1- Redundant Comment</b></h2> 

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

<h2><b>2- Poorly Written Comment</b></h2> 

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.
<b>For example:</b>

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

<b>Correction: </b>

<i>Public void DisplayTutorial()</i> //Displays window

<b>Related Article:</b> <a href="https://dev.to/hussein_cheayto/top-5-web-developers-you-should-follow-to-succeed-without-a-degree-25lg">Top 5 Web Developers You Should Follow to Succeed Without a Degree</a>

<h2><b>3- Commented-Out Code</b></h2> 
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.

<h2><b>4- Too Many Arguments</b></h2> 
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.

<b>Related Article:</b> <a href="https://dev.to/hussein_cheayto/10-people-to-follow-to-land-a-job-in-game-development-without-a-degree-4lo">10 People to Follow to Land a Job in Game Development Without a Degree</a>

<h2><b>5- One Function Multiple Tasks</b></h2>  
Ideally, every function should serve only ONE task.

<b>For instance:</b>
<i>
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;
 }
}
</i>

<b>Correction:</b>
<i>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
}
</i>
<h2><b>6- Dead Function</b></h2>
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.

<b>Related Article:</b> <a href="https://dev.to/hussein_cheayto/get-rich-while-sleeping-j3n">Get Rich While Sleeping</a>

<h2><b>7- Inconsistency</b></h2>
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.
<b>For example:</b>
Using variable names as 'Quit' and 'Exit' instead of sticking to either 'Quit' or 'Exit'.

<h2><b>8- Duplication </h2></b>
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.
<b>For example:</b>
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. 
<b>Correction</b>
Think about using polymorphism.

<b>Related Article:</b> <a href="https://dev.to/hussein_cheayto/how-to-become-successful-episode-3-bryan-guerra-4c0e">HOW TO BECOME SUCCESSFUL- EPISODE 3- BRYAN GUERRA</a>

<h2><b>9- Misplaced Responsibility</h2></b>
One of the most important decisions a software developer can make is where to put code.
<b>For example:</b> 
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?

<b>Correction</b>
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.

<h2><b>10- Function and Variable Names</h2></b>

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.
<b>Look at this code:</b>
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.

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