Code Refactoring

source: 9gag.com

What is Refactoring?

Refactoring is the process of restructuring code, while not changing its original functionality. The goal of refactoring is to improve internal code by making many small changes without altering the code’s external behavior.

TDD is very important when doing a refactoring process. It’s making sure your code behavior doesn’t change after refactoring the code.

When should code be refactored?

Refactoring can be performed after a product has been deployed, before adding updates and new features to existing code, or as a part of day-to-day programming.

When the process is performed after deployment, it is normally done before developers move on to the next project. An organization may be able to refactor more code at this point in the software delivery lifecycle, where the developers have increased availability and more time to work on the source code changes needed.

A better time to perform refactoring, though, is before adding updates or new features to existing code. When performed at this point, refactoring makes it easier for developers to build onto the existing code because they are going back and simplifying the code, making it easier to read and understand.

But in our PPL Project usually, we do a refactor after writing the code and passing the test.

What are the benefits of refactoring?

Refactoring can provide the following benefits:

  • Makes the code easier to understand and read because the goal is to simplify code and reduce complexities.
  • Improves maintainability and makes it easier to spot bugs or make further changes.
  • Encourages a more in-depth understanding of code. Developers have to think further about how their code will mix with code already in the code base.
  • Focus remains only on functionality. Not changing the code’s original functionality ensures the original project does not lose scope.

What are the challenges of refactoring?

Challenges do come with the process, however. Some of these include:

  • The process will take extra time if a development team is in a rush and refactoring is not planned for.
  • Without clear objectives, refactoring can lead to delays and extra work.
  • Refactoring cannot address software flaws by itself, as it is made to clean code and make it less complex.

PPL Implementation

Refactoring in Gin Gonic

Go is a very flexible language with so many libraries we can explore. It’s can be a benefit, but it also can be a drawback to users because it literally has no design pattern when it comes. For example, this is Gin Gonic Project if we don’t refactor the code.

Yes, it's the whole application with all the logic inside it, notice it only contains one endpoint in this application. Imagine you are working with your team and editing one file at once for the entire project. It’s very frustrating, right? That’s why for the maintainability aspect, and team aspect we need to refactor this program to be more modular and better. We need to divide this file into other small files, that have each responsibility.

Repositories

According to its name, repositories have responsibility to handle any transaction to database. We can make new file called example_repository.go to handle any transaction related to example table.

Now we have one file to handle any transaction related to database

Services

Services is a layer between controller and repositories. It helps to connect the URL with the repositories, also it can also contain any logic inside it. We can refactor our code to separate code like this.

Controller

Like any other framework, controller is used to accept requests from users and then proceed to service layer.

Now after refactoring the code into a few separate files, we can assemble it on our main.go

Any other refactoring??

You can ask your SonarQube, he knows everything 😂

Final Words

With refactoring your code it help you and your team to maintain the code and make your code more readable. Also in our case, separating our application into some files with each own responsibility, it helps the teams to work more modular and not be afraid of a lot of conflicts in one file.

Thanks for reading and don’t forget to be grateful!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store