• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many lines of code should a java function have

 
Prem Ss
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Einstein supposedly said "Make is as simple as possible, but no simpler".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic