Refactoring method

How many lines of code can a method with a good design have before you need to make the design better?

+3
source share
8 answers

Code lines are an incomplete metric used for refactoring. Cyclomatic Complexity should be used in combination with LOC as a worthy indicator for when you need to refactor.

Decent landmark

LOC > 80 || CC > 10 == "Time to refactor"

Most likely, you will encounter Cyclomatic Complexity> 10 long before you reach 80 lines of code.

Of course, there are many other indicators:

  • Afferent Link: How many methods depend on this method.
  • :
  • .
  • . Varialbes

, , , , , " 40 !"

+4

. . , 5 10- , . 50- . , , , 6 .

+3

, . , - .

:

  • 50 . , , , , .
  • , . , ; refactor, .

, . , , " ", , - .

+2

, , 25 , , , .

, 25 , , . , .

+2

, , .

, . , , . . , .

1000 , , . , , , , , . - 1000 , . .

+2

- , - . , , , , .., . , , . , , , .

:

if (color.r == color.g && color.r == color.b)
    if (color.r > 128)
       newColor = color.White;
    else
       newColor = color.Black;

if(isGray(color))
   newColor = thresholdGray(color)

, .

0

, LOC - , , ( ). - , - , . ; - , , 20 30 . , , LOC. .

LOC, - , , , .

. . LOC, . 30 , . LOC; , (, ) . , - .

0

, "", , . , LOC .

The next rule of thumb that we can follow is to limit the length of the method that we can see on one screen.

0
source

Source: https://habr.com/ru/post/1774402/


All Articles