Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the average line number per method/class?

Tags:

java

android

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
    }
});
like image 745
svenkapudija Avatar asked Feb 24 '23 23:02

svenkapudija


2 Answers

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.

like image 153
Peter Lawrey Avatar answered Mar 08 '23 15:03

Peter Lawrey


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.

like image 25
mmccomb Avatar answered Mar 08 '23 16:03

mmccomb