4

I'm interested what's the average line number per method or class. Programming language is JAVA and it's an Android project.

I know there is no specific number, but I'm interested what's the good programming practice?

EDIT: For example in android I have 10 buttons (3 for action bar, 7 for every day of the week so I can quickly choose some day of the week and get relevant information and so on, doesn't really matter what the application is about) and only this "type of code" needs ~100 lines of code (per button I need at least 10 lines of code to initialize it and set up a onClick listener), is there a way to shrink this down a bit?

someButton = (Button) findViewById(R.id.button);
someButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {               
          // do something
    }
});
svenkapudija
  • 5,128
  • 14
  • 68
  • 96
  • like 20 lines per method, 5 methods per class? – Nishant Feb 27 '11 at 09:22
  • Regardless the language, I like to see entire functions/methods on screen at once. This is pretty malleable for font size and screen size :) but I find my ability to follow flow of data and control through a function drops drastically once I have to start scrolling. – sarnold Feb 27 '11 at 09:26
  • 1
    Which is why I have a rotated screen with 1600 pixels top to bottom.;) – Peter Lawrey Feb 27 '11 at 09:28
  • +1 i found myself doing the same..its easy to see all code at once.:) – Shekhar_Pro Feb 27 '11 at 09:31

4 Answers4

4

Most analysis of this type is around cyclomatic complexity rather than the number of lines, which can change based on the complexity of each line. e.g. you can place an entire class on one line but this is not good practice.

I use a code analyzer (the one in Intellij) and if it confuses the analyzer, its likely to be confusing code. (it a form of automatic code review) It has lots of Method and Class metrics you can check, but I don't find these as useful.

Tullo_x86
  • 2,573
  • 25
  • 27
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

Since we're talking Java we're implicitly discussing OOP.

Your primary concern is therefore creating highly cohesive classes with low coupling. The number of lines in a method or methods in a class is not an indicator of either. It is however a by-product of achieving both. Your classes is much more likely to have concise well defined method that have a sole purpose if you design with these principles in mind.

In other words, don't go chasing metrics, focus on good design and principles.

If you want some hard facts then this question follows a similar track.

Community
  • 1
  • 1
mmccomb
  • 13,516
  • 5
  • 39
  • 46
1

Most of the time the answer depends on the vertical size of your screen. From the ancient times there is hidden rule of keeping the whole function in one screen, where function name, opening and closing parenthesis on separate lines. So this rule leaves about 20 lines per function with the popular 80x25 screens. This rule is still valid, if you are mostly programming on the console and your IDE is called vi. But I think this rule is still valid for other environments and higher resolutions as well.

There are other measures for understanding the complexity of a function, length being one but also the number of local variables is also another strong indicator. If you have too many, than you should consider refactoring.

But your question rather asks an advice on a certain type of function. There are cases where these rules are bent. For example functions with lots of case statements, such as automatas can be kept long.

In your case, setting up (initializing) GUI is a valid case for having 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 their own names. Then each of the buttons will eventually become only 2-lines of code. Or you can use Extract Method for each of the buttons and create them in a separate function.

Serkan
  • 1,148
  • 8
  • 8
0

as far as i know, it doesn't have anything to do with the number of lines. each method should perform a distinct operation and the focus should be on good design.

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43