当前位置:网站首页>Pits encountered in refactoring code (1)

Pits encountered in refactoring code (1)

2022-06-24 03:34:00 mariolu

One 、 The modified code has enough tests

I have to make sure that the code I'm going to modify has a reliable set of tests . These tests are essential . Following the refactoring technique as much as possible can avoid the introduction of the vast majority of bug The circumstances of , But I am human after all , After all, it is possible to make mistakes .

  • Offline test environment :
  • Online grayscale environment :
  • Online regression testing : Set the kafka Drop logs for testing

Two 、 Long function :

Algorithmic students like stacking code , Do not delete even unused code . It used to just delete an unused pointer in a structure , It saves nearly half of the memory space . Because this structure is a basic unit of the model . Loading models is a big memory consumer .

2.1 Extract Method

function /function Methods corresponding to object-oriented languages /method, Or process /procedure, Or subroutines /subroutine.

All you need to do is browse through a piece of code , Understand its role , Then refine it into a separate function , And name the function for the purpose of this code .

2.2 When to put code into a separate function , There are many different views

Some people think that considering the length of the code , There is a reuse perspective , Think of code that has been used more than once , You should put a separate function , Code that has only been used once remains inline (inline). But the most reasonable view here is to “ Intention and realization are separated ”. If you need to spend time browsing a piece of code to figure out what it is doing , Then the code should be extracted in a function . You can see the purpose of the function at a glance , Most of the time, you don't need to care about how the function achieves its purpose .

Once you accept this rule , Just form a habit : Write very small functions , Usually only a few lines .

Some people worry that short functions will cause a large number of calls and affect performance . But modern compilers are very rare , And short functions are easier to cache .

原网站

版权声明
本文为[mariolu]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210925005830773c.html