aspose file tools*
The moose likes Java in General and the fly likes How many lines of code should a java function have Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How many lines of code should a java function have" Watch "How many lines of code should a java function have" New topic
Author

How many lines of code should a java function have

Prem Ss
Greenhorn

Joined: Jul 09, 2011
Posts: 1
I am writing some complex login in a java function and my calling function has gone up to 150 lines of java code. Even after refactoring some common logic into other functions I am not able to make my calling function shorter than this. Just was wondering in a java method how many lines of code is considered to be good as per basic java coding standards. Thanks in advance

-Prem
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Prem, welcome to the Ranch!

I would consider the maximum acceptable length of a method so that the whole method fits on your screen at once in your IDE, so you shouldn't have to scroll pages and pages. Ofcourse that isn't a strict and exact rule. A method should ideally do only one thing, and not many different things. If you find that you have a method that does many different things, then it's a good idea to split it up into methods that each do one thing.

There are tools such as Checkstyle that can help you with this. Checkstyle can warn, for example, if it decides that your method becomes too complex.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

As Einstein supposedly said "Make is as simple as possible, but no simpler".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many lines of code should a java function have
 
Similar Threads
Returning error code from jar application
Timer.start issues
How to get random dropdownlist javascript value at the server site
Returning error code from jar application
how to call matlab function from a java program