7 Habits That Contributes to Code Smell


Habits of any kind are hard to break. In some cases, they become so ingrained that you might not even realize you’re doing it. In the case of software development, there are some habits that I have noticed that contribute to code smell. There are 7 habits that I noticed (some of which I’m guilty of) and that is what I’m going to share in this post.

 

code smell

 


 

1. Saying “I Will Fix It Later”

 

How often has this happened to you? You’re going through a codebase and see something that can be improved but it’s not part of what you’re focusing on so you say “I will fix it later”. Or maybe you finally got a piece of code to work after numerous tries and now your code is a bit messy and you think to yourself you’ll fix it later and continue to finish your task. The problem with this is that the opportunity to fix it often doesn’t happen and with looming deadlines, it’ll be quickly forgotten. So, next time when you say “I will fix it later” make note of it and add a TODO in the code.

 

2. Sweeping Issues Under the Rug

 

Errors happen all the time in an application when it is out in the real world. Some can be minor errors while some can be major. The important thing is that those errors need to be logged so it can be reviewed. When errors happen and they don’t get logged then no one knows there is an issue until it is too late.

 

3. Working on Your Own All the Time

 

Software development is a team sport. So, like with any team sport, it takes more than one person to build a software product. A single person can do it, but it won’t be sustainable.

 

In your team, there will probably be someone who is more knowledgeable about a topic than you. You can choose to get some help from that person or go at it by yourself. Sometimes it can turn something you’ve been working on for more than a day into a 5-minute task afterward.

 

In addition, when you communicate more with your team, you get quicker feedback. Would you rather know that you’re implementing the feature wrong at the start or at the end? When you build something for an intended purpose it is going to be far better than tweaking something to work for a certain purpose. Consider that near the end you found out you’ve been implementing the feature incorrectly so you add tweaks and patches everywhere to make it work as required. It’ll work, but it is brittle and can break with a small change.

 

4. Not Googling Enough

 

Many of the complex problems you’ll face are often already encountered by someone else. What is better is that these complex problems probably already have solutions out there such as StackOverflow. So, instead of spending all your time solving the same problem all by yourself, why not take advantage of solutions put together by a community of software developers?

 

5. Not Knowing How to Optimize

 

Not to be mistaken with premature optimization, I’m talking about using the appropriate data structures, algorithms, and timing within your implementations.

 

For example, if you’re trying to create a list of unique items. You can use a list and check for any duplicates before adding or you can use a HashSet, which would avoid duplicates without you doing anything.

 

In the case of algorithms, if you are searching for an item in an array that is sorted then you can use binary search instead of traversing the entire array. Both would work, but one is better than the other.

 

For the case of timing, it can as simple as when to make an API call. Do you make the call when all the necessary information for the call is collected or make the call when the response from the API is necessary? Should you make the API call on a separate thread and do other operations while waiting for the response to come back? You can forgo all of these decisions and simply make the API call and wait for the response. It will work, but it will not deliver a great user experience because it will be very slow.

 

6. Copy and Paste Code That You Don’t Understand

 

There is nothing wrong with copy and pasting code if you understand what it is doing. It is only an issue when you don’t understand what that piece of code is doing. When you copy and paste code that you don’t understand you are introducing unknowns into your own work. Let’s say later down the line your code broke, you would have no idea how to fix it because it is caused by something you just copy and paste.

 

7. Not Considering Trade-Offs

 

In software development, everything has a trade-off. The problem is some developers don’t consider the trade-off when things work.

 

For example, let’s say you can choose to use libraries A or B. Both libraries do the same thing but offer a different level of control. A is the easier library to work with in exchange for giving up low-level control. It is very easy to pick A and use it because it is easier and faster to work with it. However, if you’ll be doing specialized and niche things with the library then A might not meet your needs later on.


 

I hope this post was helpful to you. If you found this post helpful, share it with others so they can benefit too.

 

How many of these habits do you have? What are other habits you’ve seen that contribute to code smell?

 

To get in touch, follow me on Twitter, leave a comment, or send me an email at steven@brightdevelopers.com.


About Steven To

Steven To is a software developer that specializes in mobile development with a background in computer engineering. Beyond his passion for software development, he also has an interest in Virtual Reality, Augmented Reality, Artificial Intelligence, Personal Development, and Personal Finance. If he is not writing software, then he is out learning something new.