Somebody Called for a Cleaner: Refactoring
I have previously posted about test-driven development and clean code, tied to both and also as crucial in software development is refactoring. Refactoring is the practice of altering a software system in a way that improves its internal structure while leaving the code’s external behaviour unchanged. It’s a method of cleaning up code that reduces the risks of introducing bugs. In essence, refactoring is the process of enhancing the design of code after it has been written. Refactoring is not something that is delegated for you to do, it is simply expected of you, it is vital to the development process.
Refactoring is not always needed to achieve clean code, because some geniuses out there just write their code cleanly the first time, but its goal is to have a cleaner code. However, refactoring has its risks, as it brings changes to a proper working code it may introduce bugs. While clean code lets you know of the problems in your code, making the change to get rid of that problem is what you have to overcome in refactoring. This is where tests show their importance, including TDD as a method to test your code. Having tests gives you clear constraints on what breaks the code and how to make sure it works the same externally.
“Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.”
— Martin Fowler
Refactoring in Badaso LMS
In the development of Badaso LMS, we have done small refactors here and there to pass the pipeline, and with Aldi around, we usually clean up more than what it takes to simply pass the pipeline. Here are some refactoring examples that we have done in Badaso LMS:
I have never used Vue previously nor am I familiar with front-end development and did not know that the standard is to use 2 spaces indentation for all .vue and .js files. Going forward, this was very helpful feedback as I will still be writing on .vue and .js files for the next sprints, and it would definitely be useful to keep in mind for future projects.
What happened here was that I had a rough time getting used to our back-end stack which is Laravel (PHP), but I didn’t want to just sit anxiously trying to get a good grasp of it and stunt my overall progress. Hence, I decided to start developing the front-end part of my feature first. Since I did not have any data from any API to work with yet, I hard-coded information to fill text spaces. This required me to rework those text spaces to then be filled with proper data from the API I later made. It may look like I had to work twice, but working with the front-end first gave me a good picture of exactly what information I need and gave me a better idea of how I should implement the API.
I hope you find this blog helpful, if you find any mistakes or misinformation, I would very much appreciate the feedback. Thank you!
References
Fowler, M., & Beck, K. (2019). Refactoring: Improving the design of existing code. Addison-Wesley.
Refactoring home page. Refactoring. (n.d.). Retrieved March 22, 2022, from https://refactoring.com/
Luke illustration is a snippet from the game Eternal Return and was taken from Steam https://ristek.link/luke-model-snippet