0

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?

  • [Your peer tells you after reviewing the code.](http://programmers.stackexchange.com/a/141010/31260) – gnat Apr 27 '12 at 12:13
  • Could you please explain what do your comment and downvote mean? Thank you. –  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? –  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. –  Apr 27 '12 at 13:08
  • I didn't mean to be impolite. As for the answer I pointed to, you may be interested in a related [discussion at Programmers Meta](http://meta.programmers.stackexchange.com/questions/3472/one-line-answers) - in particular I explained there why I think this answer is good – gnat Apr 27 '12 at 13:22

1 Answers1

1

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.

CodeART
  • 3,992
  • 1
  • 20
  • 23