Re: Type Safe Collections?

Discussion in 'Java' started by Ryan Stewart, May 31, 2004.

  1. Ryan Stewart

    Ryan Stewart Guest

    "Ken" <> wrote in message
    news:...
    > It seems that java collections (other than arrays) are not type safe.
    > I say this since they hold items of type "object." IOW, the type
    > checking can't be done at compile time, and at run time it seems you
    > can't guarantee that a given collection will be used for a particular
    > data type. Is there a standard approach or pattern used to make a
    > collection type safe?
    >

    Only put whichever type you want in it?
     
    Ryan Stewart, May 31, 2004
    #1
    1. Advertising

  2. Ryan Stewart

    Chris Smith Guest

    Ryan Stewart wrote:
    > Only put whichever type you want in it?
    >


    I'm mostly in agreement with you here; I have never actually seen a bug
    due to putting the wrong type of object into a collection. To me, the
    bigger benefit of generics is:

    1. Documentation is included in the code as to exactly what you're
    passing or getting back from a method. That allows one less time of
    switching over to JavaDoc when working with an unfamiliar API.

    2. The static type knowledge can be used by a compiler to help you write
    code. For example, Eclipse currently sorts its autocompletion list so
    that when I'm calling a method that takes a specific parameter type, the
    variables in scope of that type appear at the top of the list. That
    saves me from typing the whole name or scrolling through an
    autocompletion list to look for the right value. Generics will extend
    this ability to places where it didn't previously exist.

    It's for that reason that generics can be of great benefit even when
    they aren't necessarily preventing bugs.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 31, 2004
    #2
    1. Advertising

  3. Ryan Stewart

    Roedy Green Guest

    On Mon, 31 May 2004 08:40:56 -0600, Chris Smith <>
    wrote or quoted :

    >
    >1. Documentation is included in the code as to exactly what you're
    >passing or getting back from a method. That allows one less time of
    >switching over to JavaDoc when working with an unfamiliar API.


    I remember one nightmarish project where I was trying to modify
    somebody else's commentless code. It consisted of scores of various
    collections. I had no idea what supposed to be allowed in each.


    At least Generics documents that and slaps you quickly if you get
    wrong.

    It also indicates the intent of the original programmer. In the bad
    old days, you could violate those unstated intents and the program
    would still sort of work, but would be developing entropy.

    --
    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 31, 2004
    #3
  4. Ryan Stewart

    Liz Guest

    "Roedy Green" <> wrote in message
    news:...
    > On Mon, 31 May 2004 08:40:56 -0600, Chris Smith <>
    > wrote or quoted :
    >
    > >
    > >1. Documentation is included in the code as to exactly what you're
    > >passing or getting back from a method. That allows one less time of
    > >switching over to JavaDoc when working with an unfamiliar API.

    >
    > I remember one nightmarish project where I was trying to modify
    > somebody else's commentless code. It consisted of scores of various
    > collections. I had no idea what supposed to be allowed in each.


    I had to understand a program in pl/1 once that was about 3,000 cards.
    It had only one comment: the persons last name.
    All of the data was global, and all of the functions had zero arguments.
    But it was fast.
     
    Liz, May 31, 2004
    #4
  5. Ryan Stewart

    kenandeva Guest

    Hi. Looking at Roedy's response has to do with my problem with the lack
    of type safety. It becomes even more of an issue when working in a
    safety critical environment. In that case, code must be deterministic.
    From a test perspective that means you have to test all possible paths
    in the code. If you don't have type safety, the possible paths are
    limitless, since any type can be included on the collection. In
    non-safety critical projects, that's probably okay (except for the bug
    risks pointed out by Roedy). But when you are writing software to
    control aircraft, missiles or real-time medical equipment, it becomes a
    big no-no.

    My guess is that 1.5 has type safe collections, because it also has
    generics. However, we can't use beta versions of Java, again, for
    saftey critical reasons.

    - Ken


    Ryan Stewart wrote:

    >"Ken" <> wrote in message
    >news:...
    >
    >
    >>It seems that java collections (other than arrays) are not type safe.
    >>I say this since they hold items of type "object." IOW, the type
    >>checking can't be done at compile time, and at run time it seems you
    >>can't guarantee that a given collection will be used for a particular
    >>data type. Is there a standard approach or pattern used to make a
    >>collection type safe?
    >>
    >>
    >>

    >Only put whichever type you want in it?
    >
    >
    >
    >
     
    kenandeva, Jun 1, 2004
    #5
  6. kenandeva<no spam> wrote:
    > Hi. Looking at Roedy's response has to do with my problem with the lack
    > of type safety. It becomes even more of an issue when working in a
    > safety critical environment. In that case, code must be deterministic.
    > From a test perspective that means you have to test all possible paths
    > in the code. If you don't have type safety, the possible paths are
    > limitless, since any type can be included on the collection.


    Like Chris, I have never seen bugs related to putting the wrong type of
    objects in collections.

    >
    > My guess is that 1.5 has type safe collections, because it also has
    > generics. However, we can't use beta versions of Java, again, for
    > saftey critical reasons.


    I suggest you don't use java at all, but not because of the lack of type
    safe collections. I'm not aware of any JVM that can be used in safety
    critical applications (read the Sun VM license for example.) I'm also
    not aware of any VMs designed for real-time applications.
    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Jun 1, 2004
    #6
  7. Ryan Stewart

    kk_oop Guest

    Daniel Sjöblom wrote:

    >
    > I suggest you don't use java at all, but not because of the lack of
    > type safe collections. I'm not aware of any JVM that can be used in
    > safety critical applications (read the Sun VM license for example.)


    Hi. What part of the license are you referring to? I just read the
    license, and I couldn't figure out what you meant.

    Thanks,

    Ken
     
    kk_oop, Jun 3, 2004
    #7
  8. kk_oop<no spam> wrote:
    >
    >
    > Daniel Sjöblom wrote:
    >
    >>
    >> I suggest you don't use java at all, but not because of the lack of
    >> type safe collections. I'm not aware of any JVM that can be used in
    >> safety critical applications (read the Sun VM license for example.)

    >
    >
    > Hi. What part of the license are you referring to? I just read the
    > license, and I couldn't figure out what you meant.
    >


    It seems I misremembered the license, it only says:

    Part 3, restrictions:

    ....you acknowledge that Licensed Software is not designed or intended
    for use in the design, construction, operation or maintenance of any
    nuclear facility...

    So that is one safety critical application specifically singled out.
    Also read the standard disclaimer of warranty and limitation of
    liability. If the VM crashes in a critical application it is not Sun's
    problem, but yours.

    Of course, other languages have problems, too. Compiler bugs, hardware
    bugs and hardware failures. But there is one less thing that may go
    wrong, namely that there is not a complex piece of software like a VM
    that may crash.
    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Jun 3, 2004
    #8
  9. Ryan Stewart

    Roedy Green Guest

    On Fri, 04 Jun 2004 01:22:18 +0300, Daniel Sjöblom
    <_NOSPAM> wrote or quoted :

    >
    >...you acknowledge that Licensed Software is not designed or intended
    >for use in the design, construction, operation or maintenance of any
    >nuclear facility...



    If Big Macs came with such liability waivers they would tell you they
    were not intended to be ingested.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 4, 2004
    #9
  10. Ryan Stewart

    xarax Guest

    "Daniel Sjöblom" <_NOSPAM> wrote in message
    news:40c0d1f6$0$7478$...
    > kk_oop<no spam> wrote:
    > >
    > >
    > > Daniel Sjöblom wrote:
    > >
    > >>
    > >> I suggest you don't use java at all, but not because of the lack of
    > >> type safe collections. I'm not aware of any JVM that can be used in
    > >> safety critical applications (read the Sun VM license for example.)

    > >
    > >
    > > Hi. What part of the license are you referring to? I just read the
    > > license, and I couldn't figure out what you meant.
    > >

    >
    > It seems I misremembered the license, it only says:
    >
    > Part 3, restrictions:
    >
    > ...you acknowledge that Licensed Software is not designed or intended
    > for use in the design, construction, operation or maintenance of any
    > nuclear facility...

    /snip/

    No nukes?!

    Oh, SHIT! *Now* they tell me...and my nuclear breeder
    reactor plant was almost ready to go online with a Java main
    control program (has a really cool Swing GUI, too)...eek!

    Back to the drawing board...
     
    xarax, Jun 6, 2004
    #10
    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. Doug Poland
    Replies:
    9
    Views:
    763
    VisionSet
    Sep 27, 2003
  2. Chris
    Replies:
    1
    Views:
    1,053
    Teemu Keiski
    Feb 12, 2007
  3. Royan
    Replies:
    4
    Views:
    413
    Royan
    Aug 3, 2008
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    1,394
    Gabriel Rossetti
    Aug 29, 2008
  5. mutex
    Replies:
    0
    Views:
    230
    mutex
    Jul 27, 2003
Loading...

Share This Page