Refactoring without much pain

Many developers on our team avoid refactoring. Discussion of this situation revealed the following reasons.

Why many don't refactor

  • scary to break existing functionality

  • time consuming: if the code you refactor changes, then merging (multiple times) from the main branch becomes a pain

  • there is a risk of not being in time for the task deadline

At the same time, refactoring is almost an integral part of the development process. Its need is associated with the following prerequisites:

  • initial requirements are almost never complete and development is carried out in accordance with certain assumptions, some of which subsequently turn out to be incorrect or inaccurate

  • often you need to make quick edits to get it working right here and now, without considering further support

When refactoring is warranted (needed)

I see the following reasons for changing the structure of the code:

  1. duplicate code

    There are few reasons for having duplicate code; more often than not, you need to get rid of it

  2. universalization

    To make the code easier to understand and to reduce operational problems, you can often create an abstraction that combines similar code. It is important to have at least 3 prototypes for this abstraction, otherwise it may not be accurate

    , GooglePay. , ApplePay SamsungPay . VendorPay

  3. ,

  4. /

    , 50 500 ,

: , , .

  • , . , , , , . , () , .

?

, , :

  • (unit )

  • -

  • , -

feature- master' .

, , , . , , , . , , .

- (- + ) , , ( master'), - - .

, master rebase - master.

This approach incurs some overhead, but mitigates the major refactoring difficulties:

  • reviewing and merging refactoring is not so scary, because it basically does not bring functional changes, or these functional changes are clearly visible in a small pull request

  • there is no need to support other developers' changes because the changes are merged into the main branch almost immediately

  • in the case when the deadlines are propped up, you can stop doing refactoring after the next two to three day cycle




All Articles