Importance of Refactoring Software.

Sukhrob Golibboev
3 min readMay 15, 2019

--

M ost of the software developers are not satisfied with the code they wrote in the first place and try to make it more readable or optimize it in terms of time or space complexity. Recently in Software Product Development class, I was introduced what steps to take to refactor the existing code that needs to be improved in a certain aspect of it. If you have a frustration with your old code and want to improve and make optimal please continue reading this post. I will discuss What is refactoring? Why it is important? and How I did it?

Refactoring is Cleaning? Credit: www.lynda.com

What is Refactoring?

Code refactoring is changing the structure of the pre-existing code while keeping the original behavior. When refactoring the code it refers to make changes in the nonfunctional attributes of the software. This complex procedure is aimed at modernizing software. It is typically used to increase the system’s maintainability, enhance performance, scalability, security and so on. If performed well, it can help developers discover some hidden bugs or vulnerabilities in the system.

Credits: helloacm.com

Why Refactoring matters?

You might have a project you have worked on before and never have had time or courage to go back and refactor it. because refactoring old code seems boring and time-consuming. Now we know what refactoring is. But why we even care about the refactoring a software if it is already working and doing the job. Well, there are a good number of reasons why it is important to refactor.

  • Maintainability — keeping the application maintainable will help a developer or group of developers to go back to code and do more optimization and add new features
  • Flexibility
  • Portability
  • Reusability — if your code isn't reusable or modular then you will end having bunch of lines repeated all over your file. Reusing the existing code inside another function make your code DRY(don't repeat yourself)
  • Readability — making the code readable make developers life easier. How many times you wrote a code and after a couple of days, you don't even understand what you wrote on that line?
  • Testability
  • Understandability

How I refactored my software project?

Now you might be asking how I know if I need to refactor my code or not? I got you!!! If you are a student developer like me, it is good to have someone who is a senior developer or your classmate to evaluate your product. So, I did the same I talked to my Computer science teacher to do code review on my python codesearching a substring in textthat I worked on.

Code before refactoring

We went over the old code and made some analysis and found out the algorithm I was using was more time and space. Here the exact feedback from my instructor

Since you are using string slicing each time you slice it, it is created a new substring in the memory. instead, consider using accessing each index individually(this is how it could be done with C++). It is faster and saves up the memory rather copying the string slice. This is going to be O(n) where n is the length of the substring

After the discussion, these are my takeaways to refactor and optimize my software:

  1. Put comments
  2. Reduce memory usage
  3. Remove duplicated code (Make it DRY)
  4. Make function modular
Code after refactoring

--

--