
Introduce Explaining Variable
This refactoring might be very useful when you have complex expressions in your code.
If you see an expression that is hard to understand, then this is a clear sign that "Introduce Explaining Variable" might be applied.
This way you can make a complex expression more understandable, by dividing it into its intermediate parts.
Split Temporary Variable
We use local variables to hold the result of some code - some value or some calculation result. It is an easy way to reference this result later.
If the same variable is set more than once, then it is a sign that it has more than one responsibility. In other words this variable is used for the various unrelated purposes in the code.
Such code is hard to change and maintain. To make the changes you have to recheck each case of variable use to make sure that the correct values are used.
Also the names of such variables are usually not informative.
Replace With Constant
When you have a code containing equal strings or numbers repeated then it is a good time to think that these numbers or strings can be replaced by a single constant.
So in this lesson we will talk about Replace With Constant refactoring.
Simplify Condition
Conditional expressions are used everywhere. Inside if or while statements, in return statements.
Conditions might get complex, so it is important to understand how to handle such cases and how conditional expressions can be simplified.
There are several rules we can use to reduce expressions to their simplest forms. These rules are part of Boolean algebra.
We will not cover Boolean algebra here. However we will take a look at some helpful rules.
Split and Combine Conditionals
Split conditional refactoring converts single conditional statement into several if statements.
Combine conditional refactoring is the opposite of the Split conditional refactoring.
These refactorings can be applied when you need to simple complex logical expression into several parts.
Replace Conditional With Guard Clause
Replace Conditional With Guard Clause is another useful refactoring necessary to make the code more understandable and readable. Very often we use if-else constructions to place the code execution under the conditions.
What it the logic of the conditions is complex?
Then, if we don't have the right approach to writing such constructions, then we can get situations where a large number of if-statements are nested each into other.
The final construction becomes unreadable.
Introduce Parameter Object
In this video we will discuss Introduce Parameter Object refactoring. Sometimes when you have a method that needs several parameters it might be difficult to read the method signature.
This might happen when there are five or more parameters being passed to the method. Also you might have another situation when a group of parameters is being used together in several methods in your code. This creates code duplication and it becomes harder to maintain such code.
The same group of parameters might be used in multiple methods. And these methods can be located in one class or in several classes.
Extract Method
When the code contains long methods then it is a sign it is not written correctly.
Why?
Long methods contain lots of information with a complex logic. Such methods is hard to understand and maintain.
Good programmers prefer short, well-named methods for several reasons.
- First, when a method contains a small fragment with complete logic, it increases the chances that other methods can use it.
This eliminates the duplication in the code.
- Also, it is easier to write tests for small methods and easier to maintain them.
The ExtractMethod is a key refactoring, which is good when you need to turn one large and complex method into a few more simple ones.
Extract Interface
This refactoring is simple, but it really helps to make your code design better.
Consider a situation when you have several classes which contain similar methods.
These methods might be doing the same logical thing but they have different implementations. Also you might have multiple clients using these classes.
In such cases it is advised to apply extract interface refactoring. Extract Interface will move common method declarations to a new interface.
Using interfaces instead of classes in your code is very helpful design approach.
Another case when you need to describe the operations that a class performs on its server. If it's planned to eventually allow use of servers of multiple types, all servers must implement the interface.
Extracting an interface allows isolating only common interfaces, not common code. In other words, if classes contain duplicate code, extracting the interface won't help you to remove this duplication.
Extract Class
A class should represent a single abstraction and should have a clear responsibility.
When the responsibility grows and single class starts to represent more than one abstraction then it is a wrong code design.
Such a class becomes one with many methods, many fields and properties.
This class becomes too big to understand easily.
Extract Superclass
The code duplication is one of the most serious problems in the code.
When the identical functionality has different implementation in the different parts of the code then it is a potential code smell.
One of the duplicate code cases is scenario when two or more classes has identical fields or methods.
The way to avoid such smells is applying the Extract Superclass refactoring.
Each time when you have two classes with the common fields and methods try creating a shared super class for them and move all the identical fields and methods into it.
Do you want to improve your coding skills?
The main tool great software developers use is code refactoring. In this course I will introduce you to the concept of this powerful practice.
If you have a beginner or intermediate level knowledge or even if you have great experience writing code this course would be helpful to you.
During this course you will learn the following:
Get familiar with the main types of code smells. Learn to find them in the code
You can optimize your code, make it clearer, more logical and readable
Learn about the most popular refactorings, get acquainted with their mechanics
Learn how to step-by-step apply refactorings in your code
Basic Code Smells
complex expressions and computations
multiple assignments to a temp variable
magic literals in code
hard to understand conditions
nested if statements logic
long method parameter lists
complex method bodies
classes with mixed responsibilities
What is Refactoring
Code refactoring is the process of changing your existing code so that it becomes more readable and easy to modify in future.
But these changes should preserve the existing functionality. Code refactoring is a continuous process and it is done in small incremental steps.
change of existing code
preserves functionality
continuous process
small incremental steps
Refactoring Goals
better design
reduced complexity
readability
maintainability
improved architecture
Important to Know
If refactoring is done well, it will greatly improve your code.
by simplifying the underlying logic and eliminating unnecessary levels of complexity.
If done poorly it may change functionality or introduce new bugs.
Refactorings
In this course we will discuss in detail the following refactorings:
Introduce Explaining Variable
Split Temporary Variable
Replace With Constant
Simplify Condition
Split Conditional
Combine Conditional
Replace Conditional With Guard Clause
Introduce Parameter Object
Extract Method
Extract Interface
Extract Class
Extract Super Class