G
G
graycrow2011-01-14 17:57:49
ASP.NET
graycrow, 2011-01-14 17:57:49

What is the size of the method/function "in screens" considered normal?

Several times I saw comments on Habré where it was stated that if your methods / functions have a size (in vertical lines) greater than x * screen size (where x = 0.5..1) then you are a bad programmer writing bad code.
How relevant is this statement? I'm writing in ASP.NET C# for "enterprise" and some methods take up two or three screens, because these are all sorts of checks for all sorts of stupid conditions, data access, output and formatting, etc., which I would be happy to break into smaller pieces, but it will not look very good - you will not assign values ​​to controls 1..10 in one method and 11..20 in another, for example. I understand when it is appropriate to separate some action into a separate function, but this does not always work out, as I wrote above.
In general, do you need to worry about this or can you score? How big are your features? The opinion of web developers on .Net is especially interesting.

Answer the question

In order to leave comments, you need to log in

11 answer(s)
H
holivar, 2011-01-14
@holivar

Here one habrauser generally admitted that he writes the code directly on the mobile phone ... In his case, it’s generally hard to dock with this issue

K
Kalantyr, 2011-01-14
@Kalantyr

The point of this rule is to make the method visible at a glance.
It happens - there are only five lines in the method, but at the same time there are 4 levels of nesting and such long expressions that you need to spend half an hour to understand. So the meaning is not in the number of lines, but in the total complexity of understanding the method.
In addition to the number of lines, there is also the length of the lines, it is also better to limit it. I have been writing in C# for a long time and often indent the code with empty lines, insert regions (#region). Indentation slightly increases the number of lines, while regions significantly reduce the number of lines (when collapsed).
In addition, my widescreen monitor is rotated 90 degrees, so two regular ones fit into one screen :)

K
kekoz, 2011-01-14
@kekoz

In the general case, I find long functions/methods appropriate, except for some non-trivial mathematical calculations. And in the general non-mathematical case, functions / methods longer than 50-60 lines (a couple of screens) are already starting to strain.
However, much more often it's not the number of lines that bothers you, but their content :)

X
XPilot, 2011-01-14
@XPilot

Compact!
First rule: functions should be compact. Second rule: functions should be even more compact . I can't scientifically substantiate my claim. Don't expect me to link to studies proving that very small functions are better than large ones. All I can say is that I've been writing functions of all sizes for almost four decades. I've had to create nightmarish monsters in 3000 lines. I have written countless functions ranging from 100 to 300 lines in length. And I wrote functions from 20 to 30 lines. My practical experience has taught me (through much trial and error) that functions should be very small. <...> However, lines should not be 150 characters, but functions should be 100 lines. It is desirable that the length of the function does not exceed 20 lines.
Robert Martin, "Clean Code" (although the examples are in Java, I think the book is still useful).

K
Konstantin Kitmanov, 2011-01-14
@k12th

Well, it's not an absolute rule.
If the size of the method is greater than a certain limit, this is just a signal that something is possible. not this way. But it could also be a false alarm.
If in doubt (like now) - ask colleagues to review.
By the way, formatting can be assigned to template engines.

M
MikhailEdoshin, 2011-01-14
@MikhailEdoshin

If the code is monotonous, then you can write a long function. I'm starting to wonder for myself if there are more than two screens (C). If anything, the Python source has a function in 2000 lines :) At the same time, Python is a very good example of C code.

A
Antelle, 2011-01-14
@Antelle

such that you can describe in words what the method does, and keep it all in memory. IMHO, no more than 10-20 lines.

V
VenomBlood, 2011-01-14
@VenomBlood

I took a rule for myself - a method can have length and complexity to the extent that I do not begin to “feel” it.
While the method is not “felt” - it is quite simple, but if it can be distinguished from the others by something, then something is wrong in it.

P
Puma Thailand, 2011-01-15
@opium

In my opinion, this rule is bullshit, and it was invented by bad programmers to interfere with programmers.

P
Pavel Zagrebelin, 2011-01-15
@Zagrebelion

A lot depends on the language. Compare the two options:
c#: button.Click += (o,e) => doSomething();
java: button.addClickListener(new ClickListener(){
@Override
public void onClick(Widget sender){
doSomething();
}});
The difference in the number of lines is 5 times, but they do the same thing.

C
CyMpak, 2011-01-15
@CyMpak

One action, one method. Decomposed decomposed - several private methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question