Java Language Oracle Official Code Standard Wrapping statements

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

  • Source code and comments should generally not exceed 80 characters per line and rarely if ever exceed 100 characters per line, including indentation.

    The character limit must be judged on a case by case basis. What really matters is the semantical “density” and readability of the line. Making lines gratuitously long makes them hard to read; similarly, making “heroic attempts” to fit them into 80 columns can also make them hard to read. The flexibility outlined here aims to enable developers to avoid these extremes, not to maximize use of monitor real-estate.

  • URLs or example commands should not be wrapped.

// Ok even though it might exceed max line width when indented.
Error e = isTypeParam
        ? Errors.InvalidRepeatableAnnotationNotApplicable(targetContainerType, on)
        : Errors.InvalidRepeatableAnnotationNotApplicableInContext(targetContainerType));

// Wrapping preferable
String pretty = Stream.of(args)
                      .map(Argument::prettyPrint)
                      .collectors(joining(", "));

// Too strict interpretation of max line width. Readability suffers.
Error e = isTypeParam
        ? Errors.InvalidRepeatableAnnotationNotApplicable(
                targetContainerType, on)
        : Errors.InvalidRepeatableAnnotationNotApplicableInContext(
                targetContainerType);

// Should be wrapped even though it fits within the character limit
String pretty = Stream.of(args).map(Argument::prettyPrint).collectors(joining(", "));
  • Wrapping at a higher syntactical level is preferred over wrapping at a lower syntactical level.

  • There should be at most 1 statement per line.

  • A continuation line should be indented in one of the following four ways

    • Variant 1: With 8 extra spaces relative to the indentation of the previous line.
    • Variant 2: With 8 extra spaces relative to the starting column of the wrapped expression.
    • Variant 3: Aligned with previous sibling expression (as long as it is clear that it’s a continuation line)
    • Variant 4: Aligned with previous method call in a chained expression.


Got any Java Language Question?