GAZAR

Principal Engineer | Mentor

11 Refactoring Methods

11 Refactoring Methods

Refactoring can be tricky and critical for the project, As a developer is a must to know how to write cleaner and readable code, and I have published an article about what refactoring is here. and in this one, I am going to explain 11 methods to refactor your existing codebase.

1. EXTRACT FUNCTION

1_cYksM70139b0iClbBhJImg.webp

Functions should be no larger than fit on a screen. Small functions like this only work if the names are good, so you need to pay good attention to naming. This takes practice — but once you get good at it, this approach can make code remarkably self-documenting.

Create a new function, and name it after the intent of the function (name it by what it does, not by how it does it).

2. INLINE FUNCTION

1_iMKezYPPwmDt_3zOot25Dw.webp

I commonly use Inline Function when I see code that’s using too much indirection — when it seems that every function does simple delegation to another function, and I get lost in all the delegation

3. EXTRACT VARIABLE

1_W19FEAGPnFg4ONT_xDUUig.webp

Expressions can become very complex and hard to read. In such situations, local variables may help break the expression down into something more manageable. In particular, they give me the ability to name a part of a more complex piece of logic. This allows me to better understand the purpose of what’s happening.

If the expression appears more than once, replace each occurrence with the variable, testing after each replacement.

4. INLINE VARIABLE

1_z_emOym5tGjGXyc5geibxQ.webp

Variables provide names for expressions within a function, and as such, they are usually a Good Thing. But sometimes, the name doesn’t really communicate more than the expression itself. At other times, you may find that a variable gets in the way of refactoring the neighboring code. In these cases, it can be useful to inline the variable.

5. CHANGE FUNCTION DECLARATION

1_Ahfy56wSm_grHA62juz8qA.webp

Function declarations represent how these parts fit together — effectively, they represent the joints in our software systems. And, as with any construction, much depends on those joints. Good joints allow me to add new parts to the system easily, but bad ones are a constant source of difficulty, making it harder to figure out what the software does and how to modify it as my needs change.

The most important element of such a joint is the name of the function. A good name allows me to understand what the function does when I see it called, without seeing the code that defines its implementation.

6. ENCAPSULATE VARIABLE

1_ZX-PTkwNV4epc9-kFBk6-w.webp

If I want to move widely accessed data, often the best approach is to first encapsulate it by routing all its access through functions. That way, I turn the difficult task of reorganizing data into the simpler task of reorganizing functions.

It provides a clear point to monitor changes and use of the data; I can easily add validation or consequential logic on the updates.

7. RENAME VARIABLE

1_mGloMieb3t2s4R_NNCPDSw.webp

Naming things well is the heart of clear programming. Variables can do a lot to explain what I’m up to — if I name them well.

8. INTRODUCE PARAMETER OBJECT

1_VJuKjVl8xaPhwvkg8t5Jkw.webp

Grouping data into a structure is valuable because it makes explicit the relationship between the data items. It reduces the size of parameter lists for any function that uses the new structure. It helps consistency since all functions that use the structure will use the same names to get at its elements.

9. COMBINE FUNCTIONS INTO CLASS

1_fb5jYlOYnLHYqKDKAX8Heg.webp

Classes are a fundamental construct in most modern programming languages. They bind together data and functions into a shared environment, exposing some of that data and function to other program elements for collaboration. They are the primary construct in object-oriented languages but are also useful with other approaches too.

10. COMBINE FUNCTIONS INTO TRANSFORM

1_EU01fbwvMhBE_RZUC1TMRQ.webp

Software often involves feeding data into programs that calculate various derived information from it. These derived values may be needed in several places, and those calculations are often repeated wherever the derived data is used. I prefer to bring all of these derivations together, so I have a consistent place to find and update them and avoid any duplicate logic.

11. SPLIT PHASE

1_5yaqJV0MnyLSLaV70lMExw.webp

When I run into code that’s dealing with two different things, I look for a way to split it into separate modules. I endeavor to make this split because, if I need to make a change, I can deal with each topic separately and not have to hold both in my head together.

To the end

These are simple and basic methods that can be applied to giant codebases. The more you refactor and write cleaner code, you will find yourself in a better spot to add more features.

Flexibility and readability of code are quite important especially when lots of developers and new ones are going to work on the source.\

Quality is not an act, it is a habit. __ Aristotle

Enjoyed the article? Follow me!

Have an opinion or comment? Type it!