Why Technical Debt is not Bad Debt
Tags: technicaldebt, startup, refactor, javascript
Published: 2019-10-03 03:16:06

_Release early, release often - just don't forget to refactor ;)_

##Deliver Now
As software engineers, our main objective is to deliver. Once our code works as expected, we commit it and then run it in the [CI/CD pipeline](https://medium.com/faun/ci-cd-with-angular-8-github-and-aws-codepipeline-b83d2b01e594).

While seemingly straightforward, it’s not as simple as it may seem. There exists a constant tension in the programming world that stems from shipping fast or slow at the cost of software maintainability aka technical debt.

>**Technical debt** is opting for the easy solution now instead of using a better approach that would take longer. More often than not, many agile teams choose the former than the later.

_Deliver now and fix it later_ is a very popular approach because teams are able to reduce the time it takes to get product-to-market. It’s also great because software engineers don’t have to spend too much time developing something that might be used. For many companies, it’s a fair trade off. Nevertheless, in order to make this approach work, continuous refactoring must be done — else technical debt will accumulate.

![techdebt](https://cdn-images-1.medium.com/max/1600/0*0F_2-sq18i85W6aJ.png)<figcaption>
The bigger the technical debt, the harder it is to add features.</figcaption>

##Fix it Later
_“Fixing it later” is synonymous with code refactoring._

![refactor](https://cdn-images-1.medium.com/max/1600/0*RgMtfM4bwE26R18S.png)

>**Code refactoring** is the process of clarifying and simplifying the design of existing code, without changing its behavior. 

From the business and end user perspective, this doesn’t affect them because the product doesn’t change. However, from the developers’ perspective, refactoring reduces technical debt and increases software stability.

This makes a huge difference in larger projects.

##Code is Read More Often than it is Written
![codereview](https://cdn-images-1.medium.com/max/1600/0*zxuBJS5b62U4pjek)
There are a variety of scenarios when one should refactor.

The following is a general list of things to look for when identifying code that could use some refactoring:
- Copy & paste (duplicate) code
- Ambiguous variable names
- Unused variables / methods / classes
- Suboptimal performance
- Your method’s code is longer than your screen
- Violation of DRY, KISS, YAGNI, SOLID and other software engineering principles

_Feel free to suggest any other scenarios in the comments._

##Example of Refactoring
In this section, I will show a simple example of javascript code pre and post refactoring.

###Pre Refactoring
{% gist https://gist.github.com/songthamtung/48438f4ebb6109a321c5a9681a94656c %}
###Post Refactoring
{% gist https://gist.github.com/songthamtung/546a938a88fa5dbd34b1504e2f9e2085 %}

##Code Review
- **Results**: both example produces the same outcome, which is to unset the users rank.
- **Visually**: Pre-refactoring has a lot more code than post-refactoring.
- **Performance**: Pre-refactoring makes up to 10000 roundtrips to the database, while post-refactoring makes 1 roundtrip to the database.

Imagine if more developers joined this project and stumbled upon this code. Do you think it would be easier for them to understand and contribute to pre or post refactoring?

##Conclusion
Just as the author would improve their writing to make it easier for the readers to understand, developers should also improve their code to make it easier for other developers to understand.

My advice to those in a team where you’re constantly cranking out new features, consider setting aside a sprint or two for stability. Write some unit tests to increase your code coverage and refactor some code to make your apps more robust. I know it might seem like it’ll slow down development time, but it’ll actually increase your team’s efficiency and output in the long run.

Thanks for reading! Originally posted on [Santora Nakama](https://medium.com/santora-nakama/refactoring-code-e61ff3f32f40).