• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Modularisation of code

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

A very basic question.
In an OO language like java, how many lines of code should method ideally contain? As in what is the acceptable upper limit.

Why i am asking this is, recently, i saw a piece of code wherein there was a method with 330 lines of code! All the lines were a part of the functionality of the class (as in, they weren't mere switch-case statements, with a lot of cases that could make the method so long.)

Is this really a good coding practice?

Thanks..
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ideal would be 1 line per method, but that is obviously not attainable.
I think 330 is too long, and it ought to have been divided into smaller methods, if at all possible. I have had to write methods that long myself, but they did have switch blocks in.

I read in the Deitel C# book that a method should ideally not occupy more than ½ a page, and definitely not more than a whole page, but how big is a page?

How about "not so big that it won't fit onto a JavaRanch screen"? Other people will doubtless have different opinions.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long methods has a bad maintenence.. at first you can understand everything.. but stop work with this code and try back after a few months, i experienced myself it and its really hard to understand.

I preffer break this method on various other methods who has benn called on this method.
So you will have some smaller methods who you know what then do. Documentation should be used too.

When you need in future you can start where did you stop.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How about "not so big that it won't fit onto a JavaRanch screen"? Other people will doubtless have different opinions.


But even that will depend on your screen resolution and font size. holding cntl and scrolling my mouse wheel will quickly and drastically change how many line fit on my screen.

I'd go with something like 'as small as possible to still do something useful'.

I've never thought 1 line per method was even REMOTELY reasonable. All a method could do would be to call another method. You could never have any local variables, because the first one would suck up your one line. You'd never be able to return anything, because the return statement would be your one line. plus, one simple program would have dozens or hundreds or thousands of method calls. You're program would bloat with all the overhead of defining a method. Best case, you'd need twice as many lines - instead of

line 1
line 2
line 3

you'd need

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did say 1 line was not attainable. As Fred has pointed out, this is rarely possible, but there are 1-line methods like this:I don't think it is possible to give a hard-and-fast rule saying "not more than 20 lines," or "not more than 30 lines." If you can't see the whole method at once, whether on screen (large or small) or paper, then it is probably too long and should be broken into smaller methods.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic