Manage Learn to apply best practices and optimize your operations.

When and how to refactor code

Code refactoring seems like a surefire way to give software a fresh start. But app dev teams should only attempt it for select purposes, in certain contexts and in specific ways.

Software development is something of an art. And, of course, it's an undertaking with countless tasks, which IT...

staff members accomplish in many different ways. This vast canvas enables developers to apply personal expression and creativity when they develop a code base.

But not all code is clean, concise and efficient. Poor coding habits and weak development guidelines easily lead to cumbersome and inefficient legacy code that doesn't perform well. Plus, such practices can generate vulnerabilities and create sequences that are difficult for other developers to follow and debug. Software project teams should refactor code to optimize or modernize an application and to align the code with development best practices and business expectations.

However, refactoring, in itself, leads to numerous issues for teams, so take a cautious approach to the task.

To refactor, or not to refactor

An organization could refactor code of an entire application from start to finish. In fact, code refactoring is often the only tenable solution to deal with legacy software that is about to reach its end-of-life stage, such as when a mainframe application must move to a Windows Server platform.

In actual practice, however, it is far more common to refactor small portions of an application at a time, which limits the scope and effect of refactoring tasks. For example, an organization could refactor an application's UI to establish consistent appearance and functionality.

Regardless of the scope, refactoring represents an additional investment and risk for the business. It takes time to change existing code, and the refactored code could create new flaws. Software project teams must understand when it's time to rework questionable code -- and when to leave it be.

Here are some occasions when it could make sense to refactor legacy code.

Address chronic problems. It's vital to rectify code with a documented history of problems. If a module in the code base throws unpredictable exception errors when it interrogates certain hardware devices -- despite repeated efforts to remedy the problem -- it's time to refactor. In this example, refactoring might fundamentally rework the way in which the module queries those devices.

Improve performance. Even when software functions as intended, the code can profoundly affect its performance and resource usage, especially if it uses a function or routine constantly. Refactor code to clean up cumbersome and poorly implemented tasks. The goal in this scenario is to reduce the number of processor or I/O cycles to complete and improve both compatibility and stability. Rather than make repeated calls to obtain the same state, for example, refactored code could make a single call, with a variable to retain the state one time and then simply reuse that variable as opposed to make repeated calls.

Grab low-hanging fruit. When you identify a problem and the fix is clear -- and limited in scope -- go ahead and refactor code. Low-investment and low-risk code refactoring projects easily yield performance improvements and other benefits.

Bring uniformity. Coding is usually a team sport, with each developer responsible for separate parts of the code base. However, developers bring a unique approach and preference to their particular tasks. As the code changes over time, the overall code base becomes difficult for developers to read and understand. This confusion adds tremendous time and cost to troubleshooting and debugging tasks. Refactor code to streamline the approach and iron out irregularities, making it easier to maintain in the future.

Prepare for major moves. Extensive refactoring might be appropriate when it's time to make a major deployment change. For example, software with specific hardware dependencies might not function well when deployed within virtual machines. To maintain operations, refactor code to eliminate hardware dependencies, such as directions to check for specific processors.

Best practices for refactoring code

Coding practices, preferences and styles can vary dramatically between organizations -- or even between teams. However, these methods can boost any refactoring effort.

Limit refactoring scope. Refactoring projects should adhere to a limited, specific scope. In most cases, the refactoring process cleans up or replaces a single troubled function. Developers must understand the refactoring target within the scope of the application and resist the tendency to tinker with the underlying task. Refactoring is a fix, not development work, so avoid writing new features or actions.

Consolidate repetition. There is no value in duplicated functions to do the same thing. In these cases, eliminate or consolidate repetitious code, rather than rewriting it.

Adhere to style rules. Apply consistent coding techniques across the entire development team. Consider a function, which should only perform one task; accordingly, avoid mixing multiple tasks within the same function. You could also break apart a more complex or convoluted function into multiple individual functions. As another example, code refactoring brings all the variable and function names in a project to consistent lengths and the same logical naming convention.

Code for readers. Create the most readable code possible. For example, undertake a code refactoring project that adds meaningful comments, indents, line spacing and other readability formatting to make software code easier to comprehend.

Test refactoring. The limited scope and goals of refactoring endeavors won't prevent flaws and unintended consequences. Ensure that test processes account for refactoring changes, ferreting out unexpected and unwanted behaviors. Any refactoring changes that are outside of automated test protocols should include manual test recommendations or notations so that testers can vet them.

Use automated refactoring. Automated refactoring tools, such as those available from Eclipse IDE for Java code and the JustCode add-in for Microsoft Visual Studio, boost code uniformity and consistency. These tools check for syntax errors, spelling errors, overly complex code segments and practices that stray outside of policies. However, automated refactoring tools might not fix everything or exist for every code language.

This was last published in December 2018

Dig Deeper on Software Performance Management

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

What are your biggest concerns about a code refactor?