What is the average line number for a method / class?

I am wondering what the average line number is for a method or class . The programming language is JAVA , and the project is Android .

I know there is no specific number, but I wonder what good programming practice is ?

EDIT: For example, in android, I have 10 buttons (3 for the action bar, 7 for each day of the week, so I can quickly select some day of the week and get the relevant information, etc., it really doesn't matter, what is an application), and only this "code type" requires ~ 100 lines of code (for each button I need to initialize and configure at least 10 lines of code on a listener onClick ), is there a way to reduce this a bit?

someButton = (Button) findViewById(R.id.button); someButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do something } }); 
+4
source share
4 answers

Since we are talking about Java, we are implicitly discussing OOP.

Therefore, your main concern is to create highly connected, low-grip classes. The number of rows in a method or methods in a class is also not an indicator. It is, however, a byproduct for achieving both. Your classes are much more likely to have a clear, well-defined method that has a single purpose if you design with these principles in mind.

In other words, don't chase the metrics; focus on good design and principles.

If you need some hard facts, then this question follows a similar path.

+3
source

Most analyzes of this type are about cyclic complexity , not the number of lines, which can vary depending on the complexity of each line. for example, you can place the whole class on one line, but this is not a good practice.

I use a code analyzer (the one that comes with Intellij), and if it confuses the analyzer, then it can be misleading code. (this is a form of automatic code review). It has a lot of method and class indicators that you can check, but I don't find them useful.

+4
source

In most cases, the answer depends on the vertical size of the screen. Since ancient times, there is a hidden rule for saving the entire function on one screen , where the function name, opening and closing parentheses on separate lines. Thus, this rule leaves about 20 lines for each function with the popular 80x25 screens. This rule still applies if you mostly program on the console and your IDE is called vi. But I think this rule is still valid for other environments and higher resolutions.

There are other measures to understand the complexity of a function, the length of one, as well as the number of local variables is also another strong indicator . If you have too much, you should consider refactoring.

But your question is rather asked for advice on a certain type of function. There are times when these rules are bent. For example, functions with many case statements, such as automatas, can be stored for a long time.

In your case, setting up (initializing) the GUI is a valid case for a long function . However, if you insist on having a small and easy-to-maintain function, you can move anonymous inner classes outside of this method and give your own names. . Then each of the buttons will eventually become just a 2-line code. Or you can use the Extract method for each of the buttons and create them in a separate function.

+1
source

as far as I know, it has nothing to do with the number of rows. each method should perform a clear operation, and the focus should be on a good design.

0
source

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


All Articles