Understanding Code Smells And How Refactoring Can Help - TechTarget
Maybe your like
Refactoring techniques for code smells
Refactoring encompasses several code hygiene practices. When it comes to eliminating code smells, however, there are three particularly effective techniques: one that focuses on methods, another that focuses on the method calls and a third that focuses on classes.
Composing methods
Composing aims to eliminate redundant methods. There are two ways developers can do this:
- Break code down into smaller blocks. Isolate fragmented code, extract it and place it into a separate method.
- Identify broken or unnecessary methods, as well as the calls to those methods. Replace the method calls with the method's actual code, and then delete the original method.
Simplifying method calls
The next technique is to simplify method calls that, over time, have become buried in large amounts of code that are daunting to work with. Programmers have several ways to simplify method calls, including the following:
- Adding or removing certain parameters.
- Renaming methods with ambiguous names.
- Separating queries from the modifying component.
- Parameterizing methods and introducing parameter objects.
- Removing the methods that assign objects certain values.
- Replacing the parameter with explicit methods or calls.
Refactoring by abstraction
Finally, refactoring by abstraction comes into play when large chunks of code contain duplications or redundancies. There are two techniques that constitute this approach, both of which focus on class inheritance:
- Pull up. The code behind methods that are shared among an entire group of subclasses is extracted into a superclass.
- Push down. Method code that lives within a superclass but is only used by a few of the subclasses is pushed down to those respective subclasses.
Consider the code snippet in Figure 1 that illustrates two classes: FileLogger and DbLogger. As the names suggest, FileLogger is responsible for logging data to a file, and DbLogger logs data to a database.
The IsLogMessageValid method returns true if the log message is valid and false if it is not. In this case, a log message is not considered valid if it contains a null or empty string. Likewise, the log message is considered invalid if it contains any sensitive data, such as a Social Security or credit card number.
Unfortunately, this approach is a little redundant. Coders need to write the same logic twice -- one for each of the two classes -- to check if the log messages are valid.
A better way is to refactor these two classes and create an abstract class. In Figure 2, the IsLogMessageValid method has moved to an abstract class, which helps mitigate potential code redundancy.
Tag » Code Smells Examples Java
-
Smells In Java Code: Do You Recognize Them? - DZone
-
Identifying Code Smells In Java - Java Code Geeks - 2022
-
Code Smells - Refactoring.Guru
-
Code Smells | Baeldung On Computer Science
-
Java Static Code Analysis | Code Smell - SonarSource Rules
-
Nerdschoolbergen/code-smells - GitHub
-
Code Smell - A General Introduction And It's Type - GeeksforGeeks
-
Code Smells That Are Found The Most In Software Projects - Apiumhub
-
What Are Code Smells? | How To Detect And Remove Code ... - Codegrip
-
Code Smells In Exceptions - XP123
-
Code Quality Basics - What Are Code Smells? - Spring Boot Tutorials
-
How To Identify Code Smells - Jobsity
-
Code Smells (Part One) - ProAndroidDev
-
Automated Detection Of Code Smells Caused By Null Checking ...