Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maximum LoC in a method

 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I roughly went through Sun's coding convention but didn't come across anything that mention about the suggest or suitable number of lines per method. Should I concern about this issue? Or as long as the class and methods are cohesive, LoC in a method doesn't really matter? Otherwise, what would be the preferable number of lines in a method? And to what extend should readability goes? For instance,

Example 1:


or Example 2:


The above examples are doing the same thing. Example 1 is coded in one line but sacrifice readability. Example 2 is coded in several lines but is more readable. Any idea?
 
Andriy Pererva
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Allan

The main idea is that your code should be clear and understandable for junior programmer.
I think that example 2 is too verbose, and example 1 would be great if add extra parentheses for more readability:

 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other than creating code that is understandable by junior programmers, I'd say that it depends on the domain logic pattern you are following. For instance, if you are creating Transaction Scripts, then it is likely that your methods will have more lines of code. If you are organizing your logic in a Domain Model, then your methods will have less lines of code. But, even if you are organizing your domain logic with Transaction Scripts, we should avoid having methods too big, like, with 50 or 60 lines of code. I'd say that around 30 lines of code is a good number.

Also, for this certification, I'd say that the most appropriate way to organize domain logic is with Transaction Scripts.
 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto,

Is this the book that discuss about Transaction Scripts - http://www.amazon.com/exec/obidos/ASIN/0321127420? Wow I have so many things to learn from you guys!

Yes I heard from before that LoC in a method should be kept at around 20 to 30. But it's almost impossible to do that because if you include try catch block, conditional and iteration statements, declarations and initializations that will almost take around 15 lines of codes in a method. If the method is doing some light algorithm, it will mostly be over 30 LoC.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

To be honest: I didn't care about it at all (didn't even know it is an "issue"), just used the lines of code I needed I had a look at my Data class and most of the methods are less than 15 lines of code (thanks to the very easy approach which uses a record cache, no catching IOExceptions is needed), but methods like find and init (method that builds record cache from the database file) and stop (method that writes record cache back to the database file) exceeds with ease the 30 lines of code. Exact number of lines are 55, 76 and 44 (code comments included).

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Allan!

Is this the book that discuss about Transaction Scripts


Yep, that's the one! You know, we all always have many things to learn. After I saw Philip Wadler presenting a workshop in this year's ECOOP (where I also presented a workshop), I realized that the road of the computing world is pretty pretty long. In an edition of the MundoJava magazine (a brazilian magazine about Java, where I write articles quite often), we interviewed Alex Buckley (he is currently responsible for the Java language specification and the Java VM specification), and he said that Philip is simply the guy that came up with generics in Java! So, the most important thing is to always keep learning!

Now, some things in the code do not count as LoC. For instance:



Here's a cool tool that you can use to see how many lines of code your project has, and also how many classes it has.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic