Forum Stats

  • 3,872,124 Users
  • 2,266,390 Discussions
  • 7,911,052 Comments

Discussions

readability

843793
843793 Member Posts: 41,732 Green Ribbon
edited May 13, 2003 6:14PM in Generics
I would like some peoples comments on the readability of the Generic implementation in Java.

I have a lot of experience with templates in C++ and find that most non-trivial template classes quickly become completely unreadable by humans. Anyone who's browsed through the STL will whole-heartedly attest to this.

It's so bad that you can't even tell what methods each class has, and what they take as parameters. E.g. from algorithm, it even seems like a herculean task to figure out where the class begins and ends!

I could post some of the code here, but it's so bad that surely some would think that it's a joke. So, how does Java readability compare?



«1

Comments

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    So far, I haven't seen any problem with the readability of Java's generics. Since I've never seen inside the STL, perhaps you could post a sample to show what you mean?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Since all Java classes inherit from Object and the current Collection set is using Object, it shouldn't be as messy. In c++ they have lots of methods to do different things because of the possibilities. In java we are really just concerned about converting Object to something more specific, thats straightforward.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    I agree. It's reasonable want to write Matrix not ArrayList<ArrayList<Float>>.

    At the end of the JSR-14 spec I remember they proposed adding some kind of typedef-like syntax, just for giving a class an alternative name. I can't find it now so maybe they've dropped the idea. Something like this:
    class Matrix ArrayList<ArrayList<Float>>

    Presumably you could simulate this by inheritance:
    class Matrix extends ArrayList<ArrayList<Float>> {
    }

    Or would this have some bad implications?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Yes, STL code is often mind-bending and nearly comprehensible. But how many people need to read the source? I did because we did so in an STL class, but I never had to when I programmed STL code.

    In any case, Java generics are totally different from C++ templates. Most "generic" code is no different from non-generic code. For example, the prototype compiler uses exactly the same source code for the Collections classes, just with generic class and method signatures retrofitted in. I've never seen generic Java code that resembles the puzzling STL code.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    I'm going to resurrect this thread because I've been wondering about the same thing myself.
    I agree. It's reasonable want to write Matrix not
    ArrayList<ArrayList<Float>>.
    Not only is "ArrayList<ArrayList<Float>>" tedious and ugly to write everywhere, "Matrix" is far more expressive about what these kind of variables are used for than is "ArrayList<ArrayList<Float>>".

    >
    At the end of the JSR-14 spec I remember they proposed
    adding some kind of typedef-like syntax, just for
    giving a class an alternative name. I can't find it
    now so maybe they've dropped the idea. Something like
    this:
    class Matrix ArrayList<ArrayList<Float>>
    For some reason, I haven't seen this topic come up very often, but I would think it would be fundamental to the concept. Can someone tell me why this has been left out of the spec?

    >
    Presumably you could simulate this by inheritance:
    class Matrix extends ArrayList<ArrayList<Float>> {
    }

    Or would this have some bad implications?
    I thought of this workaround as well, but it does have the bad side effect of not making the two types equivalent (as typedef in C++ does).
    ArrayList<ArrayList<Float>> base = new Matrix(); // Would work
    Matrix derived = new ArrayList<ArrayList<Float>>(); // Would not work
    Please note that I have a very rudimentary understanding of how generics would work. Maybe I am mistaken?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    schapel wrote:

    Yes, STL code is often mind-bending and nearly comprehensible. But how many people need to read the source?

    People don't read STL source - they read documentation instead, for example, http://www.sgi.com/tech/stl/ In fact, the commercial implementations of STL are purposely run through an obfuscator to make the source difficult to understand. The vendors would have much preferred a link-model for templates, but that's not easily done with C++. So, they're stuck with the source-inclusion model which requires them to expose their implementation. Hence the use of obfuscation to protect their "IP".

    In any case, Java generics are totally different from C++ templates.

    This needs to go in a FAQ in big bold letters somewhere. With C++ templates, you can express a program that computes Netwon's method at compile-time. With Java Generics, you can safely type-cast from Object to String.

    God bless,
    -Toby Reyelts

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Generally speaking, typedef's have had a bad reputation, because many C and C++ programmer's have abused them terribly.

    If Java typedefs only worked for generic classes, I think most people would be happy, even though they would still have the potential for abuse.

    God bless,
    -Toby Reyelts
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Generally speaking, typedef's have had a bad
    reputation, because many C and C++ programmer's have
    abused them terribly.
    Even the main method can be and is horribly abused. And, as has been pointed out frequently on this forum, the whole generics mechanism has abuse potential. As long as there are bad programmers, there's going to be bad code. But since the decision to add generics has been justified for the sake of type safety, some sort of typedef should be added for the sake of readability. The generics proposal loses some of its appeal otherwise, especially since one of the stated goals of the 1.5 release is to improve code readability.
    If Java typedefs only worked for generic classes, I
    think most people would be happy, even though they
    would still have the potential for abuse.
    Yes, it should be much more limited than what C++ allows. Only generic types can be aliased, and only allow one level deep of aliasing (i.e. no typedefs of typedefs). This would also fit in with the requirement to keep the implementation entirely in the compiler as sort of a preprocessing directive.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    If Java typedefs only worked for generic classes, I
    think most people would be happy, even though they
    would still have the potential for abuse.
    Yes, it should be much more limited than what C++
    allows. Only generic types can be aliased, and only
    allow one level deep of aliasing (i.e. no typedefs of
    typedefs). This would also fit in with the requirement
    to keep the implementation entirely in the compiler as
    sort of a preprocessing directive.
    Java already has a limited form of typedef -- it's called "import". And it's being extended for JDK 1.5.

    Some version for generics would be nice.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    The closest thing to Java's import in C++ is using, not typedef. You can use the using directive to import types or entire namespaces into the current namespace. It operates nearly identically to Java's import statement.

    Typedef is different in that it creates a new name for an existing type. Java doesn't have any feature of any similarity.

    God bless,
    -Toby Reyelts

This discussion has been closed.