Code refactoring in Delphi
Overview
It is rarely possible to make code structured and easy to read from the very beginning. Sometimes programmers have tight deadlines to fulfill the set tasks, find bugs that must be fixed quickly, or perform last-minute maintenance. As a result, even if the code works correctly and has a well-assembled structure, it ends up being confusing.
Refactoring is the process of changing software in a way so that it does not compromise its functioning but improves its internal part. It is used to maintain a well-designed software even with the passage of time and the changes that it will undergo. With that in mind, to facilitate the understanding of this concept, we will demonstrate the main features of refactoring, the benefits of using it, and several techniques that, when used together, ensure a gain in productivity in possible maintenance of the source code.
When should you consider software refactoring?
At a certain point in the development phase, understanding the code may become so confusing and/or complex that it starts to get in the way of developing new features. At this point, the developer may spend more time fixing a bug caused by poorly written code than adding new functionality.
A good practice to prevent this from happening is to make changes to the code, that is, refactorings, whenever you find something that deviates from the standards (object orientation, the framework used, the company’s standards, among others) so that it keeps it simple and organized.
One of the best moments to refactor code is before adding any new content to it, or before doing any maintenance. When you fix the code before delivering some new functionality, you provide your client with the software of much higher quality. The software is easier to understand for other developers, the project is improved, and it is easier to find bugs. Software refactoring is a technique that can, and should, be used for several reasons.
Another moment to refactor the code would be when the software is delivered to the customer. This way, developers will have more time to work on the code that is already written.
When you don’t need refactoring?
There are cases when the code must be completely reworked. When the code is already very confusing and it will take longer to refactor it than to rewrite, it would be the best idea to use the RE-ENGINEERING method.
The RE-ENGINEERING method consists of rewriting the code, changing its structure, and changing its functioning. For example, porting, migrating, and rewriting.
How to refactor code? (Refactoring Techniques)
The techniques used are aimed at making the code cleaner and more readable for those who are going to maintain it.
The techniques that can be used vary from code to code. All of them have their own peculiarities and issues. Here are some techniques that are often applied for code refactoring:
Red – Green – Refactor Method
- Stop and consider what should be developed. [RED]
- Let the software pass basic testing. [GREEN]
- Implement improvements. [REFACTOR]
Extraction Method
It is used to remove repeated code by grouping its parts. This new method is called in the functions where the code can be repeated.
Inline Method
Basically, it’s the opposite of what the extraction method offers. It is designed to solve the problem of when a method body becomes more readable than the method itself.
Extract Class Method
When you have a class that is performing the task of two, create a new class, then move all relevant attributes to this new class.
Rename Method
When the name of a method does not reveal its intention, it must be changed immediately, so that the new name must clearly describe what it proposes to do.
The Benefits of Refactoring
1. Cleaner code
Remove duplicate code parts and very long expressions, and rename variables. When you apply these techniques, you eliminate a lot of dirt from the code, making it cleaner and more readable.
When you have readable and clean code, it is much easier to maintain it, implement new functions, and fix bugs. Code refactoring also helps to understand some unknown software.
2. Improved application performance
An application that has no unnecessary classes, functions, variables, methods, or any other clutter runs faster and smoother. Application performance increases if the code is regularly updated or recently updated. Your app generates fast responses and users no longer complain about slow performance. This leads to a better customer experience
3. Easier maintenance (Save time and money)
Clean code takes less time to fix or implement new functions. There are no developers who love to work with unclear code or waste time figuring out some confusing moments. It takes longer to extend or update an application that is not refactored.
4. Easier bug finding
It’s easier to find bugs and fix them when the code is cleaner and more understandable.
Code refactoring isn’t just about improving code. It is a healthy habit for any company that should be adopted to avoid major issues or bugs in the app. A project lives longer if it is regularly updated or refactored. Your app will run faster and provide a better customer experience, which is important for any business.
Code Refactoring Example
The IDE of Delphi has a wide range of in-built refactoring functionality that enables you to easily and quickly refactor your code.
It provides you with the possibility to rearrange the code in a way that will make it easier to read, more efficient, and more maintainable.
In this example, we have a simple application that allows calculating how many
days have passed or how many hours have passed since the particular moment based on the parameters entered (days, months, and year). We will identify what should be refactored to improve the functionality and readability of our application.
To improve the structure of the code and keep it simple to read, we must use suggestive names for the variables, so that other programmers will be able to read and understand what is written. In this example, we can see that the variables have simple names but they are not suggestive. In this way, we can apply the “Rename” method from the IDE’s Refactor menu.
To change the name of the variable, we must select it, go to the menu Refactor > Rename. We can also use the shortcut keys: Shift + Ctrl + E, or right-click on the variable, and click Rename.
By checking the “View references before refactoring” checkbox, we will see a screen showing all the places where that selected variable is.
In this case, we change the names of variables D,M,Y for FDay, FMonth and FYear, placing the class letter at the beginning of each name.
In this example, we see that the code is duplicated in both procedures.
Then we can apply the extract method, where we will move the whole part of the code to a separate method. This way, a single function will be called in both procedures.
To execute the extraction method, we must select the part of the code that will be used to create a new function. Then we must go to the menu Refactor > Extract Method. We can also use the shortcut keys: Shift + Ctrl + M, or right-click on the variable, and click Extract Method.
After that, we must define a name for the new procedure. Here, we can see how the procedure will look after being created.
We created a function called GetDataInfo, and applied it to both procedures to eliminate code duplication.
After using some of the refactoring methods, we already have a cleaner code, with a better structure that is much easier to read.