Size of constructors

B

Ben Wilson

Hi,

Does anybody have any information about good object-oriented design
principles in terms of the size of constructors, or how large they can be,
or limits on how much you would do in a constructor until it turns out to be
"bad design"?

Can a constructor be too large, and if so what criteria would you use to
measure this?

Thanks,

Ben.
 
R

Ryan Stewart

Ben Wilson said:
Hi,

Does anybody have any information about good object-oriented design
principles in terms of the size of constructors, or how large they can be,
or limits on how much you would do in a constructor until it turns out to be
"bad design"?

Can a constructor be too large, and if so what criteria would you use to
measure this?

Thanks,

Ben.
It'd be hard to say, "If a constructor is XX lines, it's too big." A
constructor should be long enough to do what it needs to and no more. What
it needs to do is provide you a useable object, by which I mean an object
not in an invalid state.
 
A

Arvind

In most cases, follow the paradigm of any given method being readable
in 'window pane' without having to scroll down ! :)

This would imply that it follows the KISS concept - keep it simple &
stupid...

Disclaimer :
BTB, Kiss has nothing to do with anyone being stupid ! ;)

It should do what it is meant to do - without overdoing what it is not
meant to !

Arvind
 
F

FISH

Ben Wilson said:
Hi,

Does anybody have any information about good object-oriented design
principles in terms of the size of constructors, or how large they can be,
or limits on how much you would do in a constructor until it turns out to be
"bad design"?

Can a constructor be too large, and if so what criteria would you use to
measure this?


There is a limit IIRC on the size of methods in Java bytecode (64k?).
Apart from that I don't think there's a limit, as such. However,
programmers are used to constructors returning 'immediately', so
if a particular constructor you have written takes a while to run
you may wish to place a warning in the Javadocs (or consider firing
off a thread, or simply removing the time-consuming code into a
second method).


-FISH- ><>
 
J

John C. Bollinger

Ben said:
Hi,

Does anybody have any information about good object-oriented design
principles in terms of the size of constructors, or how large they can be,
or limits on how much you would do in a constructor until it turns out to be
"bad design"?

Constructors should initialize the constructed object to a valid state
(based on their arguments) and no more. If you find that you want or
need to perform a lot of work when you create a new instance then you
may want to consider using a factory method or a seperate factory class
instead. That's partly because of convention, but also partly because
it si possible to get yourself into trouble with the semantics of
constructors.
Can a constructor be too large, and if so what criteria would you use to
measure this?

Yes it can, but the criteria are largely subjective, assuming that you
are not running into any of the limits imposed by the class file format.


John Bollinger
(e-mail address removed)
 
R

Roedy Green

There is a limit IIRC on the size of methods in Java bytecode (64k?).
Apart from that I don't think there's a limit, as such
That limit would come from 16 bit jump offsets in byte code. If they
are signed, that would limit you to 32K. You would never write code
this long manually. But you might generate it mechanically.
 
R

Roedy Green

That limit would come from 16 bit jump offsets in byte code. If they
are signed, that would limit you to 32K. You would never write code
this long manually. But you might generate it mechanically.

even if there signed, if you allowed wrap around, you could still have
64K.
 
F

FISH

That limit would come from 16 bit jump offsets in byte code. If they
are signed, that would limit you to 32K. You would never write code
this long manually. But you might generate it mechanically.

Its been so long since I did any hacking around with class file
generation that I must admit I have totally forgotten what the
limits are. I know that *somewhere* there is a 64k limit caused,
as you noted, by a 16 bit unsigned value in the class structure -
it may indeed be the entire class itself is limited to 64k.

The jump offsets would only impose a limit of 32k in either direction.
So it would be possible (perhaps?) to have a method greater than 32k
providing a single loop/branch construct didn't span more than 32k.

Not very desirable, though... :)

How big are the entries in the exception and linenum tables? If they
are only 16 bits (as I suspect they will be) then that will places a
limit on the method size.

Answers on a postcard please.... ;-)


-FISH- ><>
 
M

Michael Borgwardt

FISH said:
Its been so long since I did any hacking around with class file
generation that I must admit I have totally forgotten what the
limits are. I know that *somewhere* there is a 64k limit caused,
as you noted, by a 16 bit unsigned value in the class structure -
it may indeed be the entire class itself is limited to 64k.

No, it's per method. Here's a list of all restrictions imposed by
the class file format:

http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88659
 
H

Hylander

Ben Wilson said:
Hi,

Does anybody have any information about good object-oriented design
principles in terms of the size of constructors, or how large they can be,
or limits on how much you would do in a constructor until it turns out to be
"bad design"?

Can a constructor be too large, and if so what criteria would you use to
measure this?

Thanks,

Ben.

Signature or body? Regarding signature, you can always turn parameters
that turn up frequently together into a kind of value object. (and
hence the body will likely be small too). KISS principle is a good
one.

I believe man was given 10 fingers for a reason. Aim to keep all
things at about 10 elements (to be flexible, this really means maybe
as low as 2 though or up to 20 but around that order of scale) or less
and you'll have a fairly simple design.

You can get code metric tools to analyze your code for complexity and
I believe this is one of the criteria used to determine it. Also,
limit the number of control structures/blocks in code to around 0-3
and in particular avoid nesting and you will have simple code.

HTH,

J
 
M

Michael Borgwardt

Dale said:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88659

Andbe aware that the 1.5 VM is supposed to remove many of those
restrictions. I don't have any of the details and I'm not online
right now to point to the information, but it is in the docs for
1.5 somewhere.

I can't find it anywhere in the release notes:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

But according to JSR 202 it should be in there:
http://www.jcp.org/en/jsr/detail?id=202

But there doesn't seem to be an actual specification of the
changes anywhere. Or am I too stupid to find it?
 
C

Chris Uppal

Michael said:
Andbe aware that the 1.5 VM is supposed to remove many of those
restrictions. I don't have any of the details and I'm not online
right now to point to the information, but it is in the docs for
1.5 somewhere.
[...]
But there doesn't seem to be an actual specification of the
changes anywhere. Or am I too stupid to find it?

I've looked unsuccessfully too.

It'll obviously require a breaking change to the .CLASS file format. FWIW, my
home brewed classfile parser works fine on all the files in the 1.5.0(beta1)
rt.jar so they are apparently all in "old" format.

Also given a moderately large method:

public class Big
{
public int[]
method()
{
return new int[]
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
[...snipped...]
99980,99981,99982,99983,99984,99985,99986,99987,99988,99989,
99990,99991,99992,99993,99994,99995,99996,99997,99998,99999
};
}
}

Trying to compile it with:

javac -source 1.5 -target 1.5 Big.java

fails with a "too many constants" error.

Doesn't prove anything of course -- I want to see the /spec/ -- but I doubt if
it's implemented in this beta.

-- chris
 
N

Neal Gafter

Dale said:
And be aware that the 1.5 VM is supposed to remove many of those
restrictions. I don't have any of the details and I'm not online
right now to point to the information, but it is in the docs for
1.5 somewhere.

JDK1.5 does not relax any of the limits.
 
D

Dale King

Hello, Michael Borgwardt !
You said:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.
oc.html#88659

I can't find it anywhere in the release notes:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

But according to JSR 202 it should be in there:
http://www.jcp.org/en/jsr/detail?id=202

But there doesn't seem to be an actual specification of the
changes anywhere. Or am I too stupid to find it?

I guess I stand corrected (actually I'm sitting corrected). My
information about it being in 1.5 is out of date. Obviously from
JSR202 it was originally supposed to be in 1.5, but unfortunately
1.5 is a moving target. If you look at the release history of the
contents for 1.5 (JSR 176) you will see that it was in, taken
out, put back in, and then finally taken back out again. Guess i
just hadn't kept up with the changes in this regard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top