• Some Guy (unregistered) in reply to seer

    On the third day of Christmas, my true love gave to me a ternary in a pair tree ...

     

  • (cs) in reply to dmilor
    dmilor:
    brendan:

    I'd rather go with

        public String toString() {
            return "Pair(" + t + ", " + e + ")";
        }

    yours is just to hard to read :), God invented operators for a reason.


     

    Yours is just as inefficient as the code I quoted above i.e. it is exactly the same code just formatted differently. Granted efficiency probably isn't a huge concern in a toString() method but I'd hardly say append().append().toString() is too hard to read. And if it you find it too hard too read then maybe Java isn't for you. ;)


    Further reading on StringBuffer.append() vs String + can be found here. String + stinks. Mightily.

     

    I'll give you some slack since you've stated you haven't done Java in a while, but you really should read that web page again.  The page actually proves that these two lines have nearly indistinguishable performance:

            return "Pair(" + t + ", " + e + ")";
            return new StringBuffer("Pair(").append(t).append(", ").append(e).append(")").toString();

    Read the page more closely.  StringBuffer is appropriate when you have repeated append operations, such as in a loop.

    It is generally not appropriate to second-guess Java's + operator, which in fact works very well, particularly in a toString method.

    Good programming isn't just about optimization, it's about how easy to read the code is.
     

  • kowari (unregistered) in reply to seer
    seer:
    Anonymous:
    ...it even supports nesting! ...
    A ternary pair tree? A tree-o-nulls? I love it! Actually, a proper pair-implementation could still provide the extra args

    You two OBVIOUSLY missed the real reason for the extra object. Where else would you put a partidge nest in a pair tree!

    BOOM BOOM!

  • Dan Farina (unregistered) in reply to MGS

    If it happened to be important, I can come up with a few plausable reasons:

    1. Speed. Hash tables are much slower than static field references or array references.
    2. Space. Hash tables take up a lot of overhead space to maintain O(1) behavior
    3. Sorting. Suppose you wanted to extend Pair or write a comparator for pairs and sort them in an array -- say to help you find a bounding box of a series of points. A simple map will not suffice without gymnastics
    4. Semantics. Not all double/double two-tuples serve the same purpose. If we're going to have to live with static typing, we may as well try to use it to some advantage.

    Are any of these satisfactory?

  • Lloyd (unregistered)

    WTF? I hit the random Random Article link, which then redirects me (randomly?) to this article over and over again. I know random doesn't mean different every time but, wtf?

Leave a comment on “Three's a Crowd”

Log In or post as a guest

Replying to comment #:

« Return to Article