Discussions

General J2EE: Number of methods in a class

  1. Number of methods in a class (4 messages)

    Guys,

    Just wanted to discuss, how many methods should be there in a class and how many lines should be there in each method.

    I feel 20/20 is a good count, means not more than 20 busniess methods per class and 20 lines per method. By saying 20 methods I am discounting the getter/setter methods of a java bean.

    How do you guys feel about it?

    Thanks
    Sanjay Vashisht

    Threaded Messages (4)

  2. Guys,Just wanted to discuss, how many methods should be there in a class and how many lines should be there in each method.I feel 20/20 is a good count, means not more than 20 busniess methods per class and 20 lines per method. By saying 20 methods I am discounting the getter/setter methods of a java bean.How do you guys feel about it?ThanksSanjay Vashisht
    I think 20 lines per method is a bit optimistic, I would say: "30 is the reccomended, but never exceed 50".
  3. Number of methods in a class[ Go to top ]

    I try to aim for an average of 10 lines per method, which makes for lots of little, easily tested methods. If a particular method gets to be too long, I split out part of its logic into another (usually private) method.

    I don't worry so much about the total number of methods in a class. That depends too much on the nature of the class. If I do worry about it, I only worry about the number of public methods, and don't put any constraints on private methods.

    Rather than focus on total number of methods, I feel that it more important to ensure that your classes maximize cohesion. Each class should have a well defined task, and only include methods directly related to that task. Unrelated tasks should be delegated to other classes.
  4. In my opinion, ANY arbitrary number is a wrong one. What if you just HAVE to code a complex method, and find no suitable way to decompose it into a number of shorter, simpler methods? You give up on the project? You change the most effective algorithm just because it's LONG?
    There are as many criteria as programmers out there. I heard outrageous ideas like "a class must never be longer than 2 pages" (this particular one came from an excelent teacher at college). What kind of page? A4, letter? And using which font? Courier new, Tahoma of Arial? And the font's size? What if I never print my code?
    The same applies to counting lines. How many lines does this method have, for instance?

    public class SomeClass
    {
       public void SomeMethod()
       {
          int a = 0;
          int b = 1;
          if (a > b)
          {
              System.out.println("Yeah, 0 is greater than 1!");
          }
          else
          {
              System.out.println("Damn! My first grade teacher lied to me");
          }
       }
    }

    And what about this one?

    public class SomeOtherClass
    {
       public void SomeOtherMethod()
       {
          int a
              =
              0;
          int b
              =
              1;

          if (a > b)
          {
              System.
                 out.
                    println("Yeah, 0 is greater than 1!");
          }
          else
          {
              System.
                 out.
                    println("Damn! My first grade teacher lied to me");
          }
       }
    }

    Ultimately, you can write a whole class in a single line...
    Of course, there are limits. 1000 methods are just too many...
    So, MY criteria is: let the number of lines or methods be dictated by your design and algorithms, rather than some arbitrary choice. If yours is a good design, you shouldn't worry.
    Furthermore, consider this: if you are working on a project where a beautifier is applied to the source files, all your lets-take-care-of-methods-length can go down the drain FAST if the code conventions change...

    Just a thought...
  5. If the method lines are exceeding 20-25 lines and the number of methods exceeds 20 that indiactes the issue with design.


    Thanks
    Sanjay Vashisht