File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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: 14193
    
  20

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 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

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
 
GeeCON Prague 2014
 
subject: How many lines of code should a java function have