paint-brush
Declutter Your Code!by@codejedi
3,034 reads
3,034 reads

Declutter Your Code!

by Code_JediApril 15th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Often, your code's bugs can come from useless pieces of code. Code Decluttering is essentially the act of ridding your code of any un-needed code components such as variables, functions, and imports. The first method is using your editor's "find" function. The second method is manually scanning for useless code. You can use this to find and remove variables and functions that aren't referenced anywhere and replace them with new elements. The last time I was working on a data science project in Python, my code would give a "index not in range" error.
featured image - Declutter Your Code!
Code_Jedi HackerNoon profile picture

Often, your code's bugs come from useless pieces of code.


I remember the last time this was the case for me. I was working on a data science project in Python, and my code gave an "index, not in range" error. I spent about an hour making sure my indexing was right. After all, I was working with tens of thousands of rows of data, so it made sense that I thought I had made a mistake.


Long story short, using the traceback library, I pinpointed where the error was coming from. My code was using try-except, so it didn't automatically do so. It turned out that I made no mistake in the indexing. Instead, what was causing the error was one variable that used an array index of an outdated version of the array, which caused the index error. I left it in the first place because I didn't think it would cause any problems for the time being.


You Should Regularly Declutter Your Code

My little anecdote brings me to what I want to talk about in this article: Code Decluttering.

More importantly, why you should regularly declutter your code. Code Decluttering is essentially the act of ridding your code of any un-needed code components such as variables, functions, and imports.


The reason why you should regularly declutter your code is that doing so saves you from having confusing bugs that are caused by un-needed pieces of code.

How to Declutter Your Code

  1. The first method is using your editor's "find" function.


Open your code editor and open the file you'd like to declutter. Finally, use command+f or control+f to make a pop-up appear. This pop-up will let you find elements inside your file, as well as replace them with new elements. Find-ReplaceYou can use this to find and remove variables, functions, and imports that aren't referenced anywhere and find and remove code referencing outdated variables, functions, and imports.


  1. The second method is manually scanning for useless code.


About five months ago, I was building an app using an API. I used the boilerplate template provided for my programming language, so I just started building on top of it. After a week, I started getting these weird errors that I didn't understand the origin of. It didn't take me long to realize that it was being caused by a solid 15 lines of useless code that were causing the error.


I left it in in the first place because I didn't fully understand the code and thought that it was integral to the code, but after I discovered that it was the cause of the bugs I was experiencing, I carefully looked at its functionality and realized that it was useless to me.


When you have a large body of code that often changes and evolves as it does so, there's a good chance that it's going to leave out useless pieces of code that you are going to forget are there, and that might throw confusing errors in the future. This is why you should manually scan for useless code every couple of days(depending on how much your code changes).

Conclusion

Hopefully, code decluttering will help you avoid bugs in the future!