Take the 2-minute tour ×

Possible Duplicate:
What should be the maximum length of function

In Java (and I think in other OOP languages too) it is recommended to keep class methods as short as possible.

While I understand this recommendation I do not understand what is a "short-sized" method and "large-sized" method.

What is a particular criteria to determine that my java method is short enough?

share|improve this question

marked as duplicate by Doc Brown, Yannis Rizos Apr 27 '12 at 11:30


Could you please explain what do your comment and downvote mean? Thank you. –  Rafael Osipov Apr 27 '12 at 12:49
Please correct me if I am wrong. Answering my question you tell me that my peer will tell me what is a short java method after reviewing my code. Right? –  Rafael Osipov Apr 27 '12 at 13:01
Right - comment points to what I consider most reasonable answer to questions like one you asked (short enough, too long etc). As for downvote it is due to the fact that besides vague it is recommended you did not refer to any other research you did prior to asking - "low effort question" as they say it here –  gnat Apr 27 '12 at 13:03
Regarding "it is recommended" perhaps you're right. I supposed that most of users here have read classic OOP books. Regarding "Your peer tells you after reviewing the code" - it does not appear as a polite answer. Ok then, have a nice day. –  Rafael Osipov Apr 27 '12 at 13:08
show 1 more comment

1 Answer

up vote 2 down vote accepted

The cyclomatic complexity of a section of source code is the count of the number of linearly independent paths through the source code.

Ref: http://en.wikipedia.org/wiki/Cyclomatic_complexity

  • I recommend that you keep that number under 10 in a single method. If it gets to 10, then it's time to re-factor.

  • There are tools that can evaluate your code and give you a cyclomatic complexity number.

  • You should strive to integrate these tools into your build pipeline.

  • Don't literally chase a method size, but try to look at its complexity and responsibilities. If it has more than one responsibility, then it's probably a good idea to re-factor. If its cyclomatic complexity increases, then it's probably time to re-factor.

  • I'm fairly certain there are other tools that give you similar feedback, but I didn't have a chance to look into this yet.

share|improve this answer
Thank you for prompt response :) –  Rafael Osipov Apr 27 '12 at 11:35
You are welcome! –  CodeART Apr 27 '12 at 11:39
add comment
