How long should the function be (line of code in the function)?

Possible duplicate:
How many lines of code should a function / procedure / method have?

I would like to know how many lines of code should function? How many lines are too many.

I read this a while ago, it was about 10 or 20 lines, but that was because there would only be so many lines on the screen. Now, when the screen size becomes larger, this will not be confirmed.

Suppose that no part of the function is used anywhere, i.e. ignores the DRY principle.

I would like to hear what others have to say about this.

thank.

Note : Duplicate When is the function too long? , could not find it when I published.

+3
source share
10 answers

Code Complete answered this question well . Steve McConnell wrote a whole page to answer this question. His conclusion:

Decades of evidence say that routines of this length (> 100 lines) no more errors than shorter routines. Let questions such as routine cohesion, the number of decisions, the number of comments needed to explain the routine, and other complexity-related considerations dictate the duration of the procedure instead of imposing a length limit as such. However, if you want to write procedures longer than about 200 lines, be careful.

+5
source

Lines don't matter, but complexity.

, . , , .

+17

, .

(, , 10-20 - , ). , . , , //.

+4

. 20 , - . , , . SOLID , 1 ..

+2

, , .

5-10 , , () , , . , , .

, , .

+1

, , ... .

, , / .

0

, . DRY , ( ).

0

Linux :

, . ( ISO/ANSI 80x24, ), .

, , , , , : , . . - 4.

- ; , , 1-2 .

0

:

, LoC. .

a very trivial functional example would be instead of having a function that computes the inline fibonacci sequence in a loop, I would add a successor function (int a, int b) that is called by fibonacci ().

A more complex example in the mod could be an http client that executes a GET request. I would break it down into something like this:

Connection getConnection(String host, int port)
Request createRequest(String[] params)
void sendRequest(Request r)
String getResponse(Connection c,Request r)
0
source

Functions should be small enough to do their job, but no less.

0
source

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


All Articles