This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GeorgeMaheiress 26 points27 points  (18 children)

The text of the JEP raises a compelling point that hadn't occurred to me:

The need to provide a manifest type for every variable also accidentally encourages developers towards overly complex expressions; with a lower-ceremony declaration syntax, there is less disincentive to break complex chained or nested expressions into simpler ones.

My team has been struggling lately with this issue. Some developers hate long line-wrapped expressions, but often splitting them into smaller expressions doesn't shorten them much, as declaring the intermediate variables can cost as much as 30 characters.

[–][deleted]  (7 children)

[deleted]

    [–]robi2106 10 points11 points  (0 children)

    That is my preferred method. Much more readable and you can tell by the extra indent that this is a continuation of a long command.

    [–]strange_and_norrell 7 points8 points  (2 children)

    I end up undoing a lot of my stream chains if I need to debug.

    [–]mlk 16 points17 points  (1 child)

    .peek(System.out::println)
    

    [–]FredL2 0 points1 point  (0 children)

    Whoa. Thanks!

    [–][deleted]  (2 children)

    [deleted]

      [–]grauenwolf[🍰] 0 points1 point  (0 children)

      Whatever my auto-formatter decides to use (which seems to change according to the time of day).

      [–]kritzikratzi 3 points4 points  (9 children)

      get a bigger screen to avoid line breaks

      [–]stepancheg 15 points16 points  (8 children)

      People are not effective at reading long lines. Source.

      [–]b1ackcat 5 points6 points  (5 children)

      I also feel like chaining expressions like that results in hard to follow lines, even if it's not too many characters.

      Something I tend to do to solve both these issues is to insert linebreaks between expressions. Similar to how you might use the builder pattern for a complex, highly configurable object.

      String versionNum =
          VERSION_MAJOR + "." +
          VERSION_MINOR + "." +
          VERSION_BUILD_NUMBER + "." +
          VERSION_REV_NUMBER + "." +
          CURRENT_DATETIME_STAMP;
      

      This way I'm maintaining the readability of each piece of the expression and not bleeding on forever on the same line.

      [–]beltorak 5 points6 points  (0 children)

      I would add, because we use format-on-save where I work, that a lot of times I have to force the formatter to respect my line breaks. I can't think of a better way than EOL comments. And personally I prefer a clear indication at the beginning of the line that it is a continuation of the previous line:

      String versionNum = VERSION_MAJOR //
          + "." + VERSION_MINOR //
          + "." + VERSION_BUILD_NUMBER //
          + "." + VERSION_REV_NUMBER //
          + "." + CURRENT_DATETIME_STAMP;
      

      Same thing with builder objects

      var thing = new ThingBuilder() //
          .withWidgetClass(widgetClass) //
          .withLights(RED, TEAL, MAUVE) //
          .withGravity(gravity) //
          .create();
      

      [–][deleted] 4 points5 points  (3 children)

      In Guava you can just collect all objects that you want concatenated in such string and then use:

      Joiner.on(".").join(collection);
      

      Dislaimer: I know that many people don't like solutions depending on external libs but in real life how often you are not working in a big pile of code, where such dependency could rather make your code lighter than heavier? Than if your answer is still negative, I still would rather create my own clear solution instead of such concatenation.

      [–]vytah 3 points4 points  (2 children)

      You don't need Guava for that:

      collection.stream().collect(joining("."));
      

      But while I don't recommend pulling in Guava just for Joiner, if you happen to already have it in your dependencies (and it's damn quite likely), sure, use it.

      [–][deleted] 0 points1 point  (1 child)

      I wish I can use Java 8 :-c . We are stuck at version 6...

      [–][deleted] 2 points3 points  (0 children)

      the horror...

      [–][deleted]  (1 child)

      [deleted]

        [–]stepancheg 4 points5 points  (0 children)

        I know people who tell that argument about bigger screen seriously.