Sunday, July 23, 2006

Java 7 improvements

Since now seems the time to request new Java 7 improvements (see for example Bag O' Tricks) here are my requests:
  • Type aliases I really hate having to type code like
    private Map<String, List<StringFormatter<DomainThing>>> map = new HashMap<String, List<StringFormatter<DomainThing>>>();
    It is so non-DRY that my fingers ache every time I see code like this.

    Here is an alternative:

    private class DomainFormatterList :     List<StringFormatter<DomainThing>>;
    private Map<String, DomainFormatterList> map =
        new HashMap<String, DomainFormatterList>();
    Still some repetition, but a lot better already. Any ideas for improvement?
  • A non-static String.format method.
    How often did I start with a literal String only to find out I had to move the cursor back to the left and put "String.format(" in front of it? It would have been easier if the format method were an instance method. Compare:
    String.format("Message for %d %s items", 5, "red");
    "Message for %d %s items".format(5, "red");
Please share you thoughts.

6 comments:

  1. I totally agree with you on both cases....

    I'm still not sure about liking static imports or not, but (as I mentioned on the finalist weblog) for stuff like String.format they kan be quite usefull:


    import static java.lang.String.format;

    public class MyFormattingTest {
      public static void main(String... args) {
        System.out.print(format("asdf %d",1));
      }
    }

    ReplyDelete
  2. See also the related conversation on typedefs/aliases in the comments of this post on java.net. Also, this referenced item on typedefs in the Bug Parade.

    Also, side comment on how this could apply to more than generics:

    public Percent = @Range(0, 100) Double;

    Or other variations on the theme.

    But while you can't effectively use deeply nested generic collections in Java right now, I sometimes think maybe that's an okay thing. Maybe a custom domain-specific class here and there in the object graph is good sometimes.

    So I'm mixed on this issue. Just one more thing to learn, but maybe that's okay.

    As for static imports, now that they're here, I'm pretty sold on them. (If I were doing a language from scratch, I'd do everything differently. So that's my caveat. But there are lots of very different languages out there, and they don't get a lot of traction. There's something to be said for familiarity.)

    ReplyDelete
  3. Thanks Tom. I just voted for the bug.

    I really like the Percent example.

    ReplyDelete
  4. $&@#$@**# So appearently Sun has already though about it (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5072887) and rejected the idea. They probably don't care about programmer productivity. Pfew, how different that is in the Ruby environment.

    ReplyDelete
  5. It was high time the Java platform got a breather from the constant deluge of changes to the syntax and core libraries introduced in every new release.
    It destroys the coherence of the language for the sole purpose of looking more like whichever other language the person proposing the change likes best.

    If this doesn't end we may well end up with a platform that's completely useless.
    Many companies and end users are already sticking with 1.4 (or 1.3 even) for that reason (if no others, corporate sluggishness has something to do with it).

    Unless a period of consolidation is begun, it may be too late.

    ReplyDelete
  6. Thanks for your comment, though I disagree with there being a deluge of changes:
    - The changes I see to the core libraries are mostly additions and improvements. Some make the libraries MORE consistent. You notice the small inconveniences when you have to switch back to earlier versions.
    - Furthermore, I'd say Java has NOT had many syntax changes. The biggest changes are introduced in Java 5. Of those the generics are weird, as many (including me) agree.

    My humble blog tries provide input to correct one of the problem with generic. More syntax? Unfortunately yes. For the better? Maybe, maybe not.

    ReplyDelete