Size of constructors

Discussion in 'Java' started by Ben Wilson, May 8, 2004.

  1. Ben Wilson

    Ben Wilson Guest

    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.
    Ben Wilson, May 8, 2004
    #1
    1. Advertising

  2. Ben Wilson

    Ryan Stewart Guest

    "Ben Wilson" <> wrote in message
    news:409cd120$0$25085$...
    > 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.
    Ryan Stewart, May 8, 2004
    #2
    1. Advertising

  3. Ben Wilson

    Arvind Guest

    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

    "Ryan Stewart" <> wrote in message news:<>...
    > "Ben Wilson" <> wrote in message
    > news:409cd120$0$25085$...
    > > 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.
    Arvind, May 9, 2004
    #3
  4. Ben Wilson

    FISH Guest

    "Ben Wilson" <> wrote in message news:<409cd120$0$25085$>...
    > 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- ><>
    FISH, May 10, 2004
    #4
  5. Ben Wilson wrote:

    > 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
    John C. Bollinger, May 10, 2004
    #5
  6. Ben Wilson

    Roedy Green Guest

    On 10 May 2004 04:52:49 -0700, (FISH) wrote or
    quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 10, 2004
    #6
  7. Ben Wilson

    Roedy Green Guest

    On Mon, 10 May 2004 22:08:04 GMT, Roedy Green
    <> wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 10, 2004
    #7
  8. Ben Wilson

    FISH Guest

    Roedy Green <> wrote in message news:<>...
    > On 10 May 2004 04:52:49 -0700, (FISH) wrote or
    > quoted :
    >
    > >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.


    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- ><>
    FISH, May 11, 2004
    #8
  9. FISH wrote:

    >>>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.

    >
    >
    > 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
    Michael Borgwardt, May 11, 2004
    #9
  10. Ben Wilson

    Hylander Guest

    "Ben Wilson" <> wrote in message news:<409cd120$0$25085$>...
    > 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
    Hylander, May 11, 2004
    #10
  11. Dale King wrote:
    >>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
    >
    > 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?
    Michael Borgwardt, May 14, 2004
    #11
  12. Ben Wilson

    Chris Uppal Guest

    Michael Borgwardt wrote:

    > > 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
    Chris Uppal, May 14, 2004
    #12
  13. Ben Wilson

    Neal Gafter Guest

    Dale King wrote:
    > 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.
    Neal Gafter, May 14, 2004
    #13
  14. Ben Wilson

    Dale King Guest

    Hello, Michael Borgwardt !
    You wrote:

    > FISH wrote:
    >
    > >>>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.

    > >
    > >
    > > 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.
    oc.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.
    --
    Dale King
    My Blog: http://daleking.homedns.org/Blog
    Dale King, Apr 15, 2006
    #14
  15. Ben Wilson

    Dale King Guest

    Hello, Michael Borgwardt !
    You wrote:

    > Dale King wrote:
    > >>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.
    oc.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?


    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.
    --
    Dale King
    My Blog: http://daleking.homedns.org/Blog
    Dale King, Apr 15, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,271
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    586
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    595
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    392
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    464
Loading...

Share This Page