Some popular Refactoring techniques

Tram Ho

In the process of code, I find that with functions that are written too many lines, especially in the function, many functions often cause many problems such as:

  • Difficult to control, difficult to reuse and easily cause bugs because handling too much logic in the function
  • It takes a long time for review to affect the progress of the project

In addition, there are some other issues such as comments, criticism of stupid code. And to partly limit these problems, I would like to introduce to you three common techniques commonly used to re-structure the function without changing its behavior. Let’s follow and apply you guys. Let’s go!

Technique 1: Extract Method

Understanding this technique is to separate the code in a bloated function into smaller functions that only perform one of your functions.

Let’s look at an example:

We can separate the processing of the current date into a separate function as follows:

Now it seems to be easier to read, but we can add some more functions to try it out:

It seems to be longer, but the functions it explicitly looks easier to read, don’t you.

Technique 2: Refactoring Conditionals

You can also refactor complex conditions into methods to make them easier to read.

Let’s look at an example:

Are you familiar with how to set this condition?

It’s easy to see the second part of an if condition is not easy to read, so we should split it into a method:

It’s easier to read, right? This makes things much easier for those who read this code in the future.

Code refactoring
Summary of Agile method

Technique 3: Replace Method with Method Object

Sometimes you have a big way out of control. In this case it may be difficult to refactoring because large methods tend to have many local variables. One solution is to use Method Object refactoring technique.

Let’s look at an example:

To perform refactoring, we can create a new class and convert local variables into instance variables. This helps us refactor this code without worrying about data transmission.

This is the MailSender class after being restructured:

And this is the new class that we define:

Is it really clear to see you?

Conclusion

The above are just three of many techniques to re-structure the code. Using these techniques will help you follow the Single Responibility Principle principle and keep your classes and methods in check.

If you like this post, share it with your friends so they can enjoy it too!

Article source: Introduction to Refactoring

These are basic Machine Learning techniques that help computers think like people
Technical Analysis Techniques
Share the news now

Source : viblo .u9515bddeddf9faba24e18925b3ecd0a6 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u9515bddeddf9faba24e18925b3ecd0a6:active, .u9515bddeddf9faba24e18925b3ecd0a6:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u9515bddeddf9faba24e18925b3ecd0a6 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u9515bddeddf9faba24e18925b3ecd0a6 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u9515bddeddf9faba24e18925b3ecd0a6 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u9515bddeddf9faba24e18925b3ecd0a6:hover .postTitle { text-decoration: underline!important; } Đây là những kỹ thuật Machine Learning cơ bản giúp máy tính tư duy như con người .u1ecb3f3b6888f2d84fc82a77ee60a0b5 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u1ecb3f3b6888f2d84fc82a77ee60a0b5:active, .u1ecb3f3b6888f2d84fc82a77ee60a0b5:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u1ecb3f3b6888f2d84fc82a77ee60a0b5 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u1ecb3f3b6888f2d84fc82a77ee60a0b5 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u1ecb3f3b6888f2d84fc82a77ee60a0b5 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u1ecb3f3b6888f2d84fc82a77ee60a0b5:hover .postTitle { text-decoration: underline!important; } Kỹ Thuật Phân Tích Giải Thuật