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?

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Could you please explain what do your comment and downvote mean? Thank you. – user32666 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? – user32666 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. – user32666 Apr 27 '12 at 13:08

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.