“The predominant method is, we will write a whole bunch of tests around the code to guarantee all of its behavior, and then we are free to edit the code however we want because those tests will assure we didn’t change the behavior,” he said. “The problem with that method is it is pretty extensive to write all those tests first, and it is still prone to error because the only mistakes you will catch are the ones you thought to look for, the ones you wrote the test for, and all the unknowns will bite you.”

The manual way is also extremely susceptible to mistakes because it involves a large amount of copying and pasting, moving lines of code up and down and back and forth between different files. “When that is being done manually is it easy to paste something in the wrong place, forget you copied something, and you miss it and it fails to appear in the other place where it was supposed to be,” said Pluralsight’s Grosenbach.

Belshee believes an automated transformation approach that involves automated and assisted code transformation tools is the best and fastest way to do code refactoring. “If you do the automated transformation, and you use a set that only allows true refactoring, then when you are executing a transformation, your IDE is statically analyzing the code and guaranteeing that it is only going to give you the transformations that have exactly the same behavior and you know you are bug-to-bug compatible,” he said. “Those transformations take about a quarter of a second to do where doing that much transformation by hand would be a two-minute task once you are an expert at it, a five to 10 minute task when you aren’t an expert.”

To achieve this type of automated transformation, Belshee says developers should be looking for a fully automated tool in an IDE that has full static awareness and has the core set of refactoring values. According to Belshee, there are 11 refactorings that come in two sets: the core six and the key five. The core six includes a set of refactoring that allows developers to name concepts to make code legible and understandable. They include rename, introduce method, introduce variable, introduce parameter, introduce field, and inline. The key five include refactorings that developers need to use to make code testable, and they include introduce parameter object, move method, make method static, convert to instance method, and spilt class.

“You can accomplish nearly any design change using combinations of only these key 11,” said Belshee. “There are over 100 named refactorings, but the rest handle edge cases.”

In addition, JetBrains’ Hariri says tools should provide more than just search and replace; they should be able to detect potential issues and be able to inspire confidence in developers.

“Nothing is worse than having a tool you don’t trust when refactoring, because then you end up doing a half job,” he said. “Tooling plays an important role in removing the grunt work and avoiding mistakes. As humans we tend to make mistakes, especially when we repeat routine patterns. We become careless. Having tools to do this kind of work for us is much more efficient.”

Whether developers decide to use a tool or not, Grosenbach believes refactoring is an important skill developers should learn and take throughout their entire career.

“A lot of people have this concept that engineers will just come up with an idea and it is perfect and works well,” he said. “That is not the way it works. There are a lot of tweaks and improvements throughout the process, and refactoring is at the heart of it.

“To me, it is an extremely important skill, extremely important for the business because if you have a codebase that isn’t easy to understand or not something a developer can easily work on and improve, then you are just throwing [away] tens of thousands or even millions of dollars on your investment in technology and software.”

About Christina Cardoza

Christina Cardoza, formerly known as Christina Mulligan, is the Online & Social Media Editor of SD Times. She covers agile, DevOps, AI, machine learning, mixed reality and software security. Follow her on Twitter at @chriscatdoza!