Line wrapping: Style guides say "prefer to break at a higher syntactic level"

I work at Google. Our style guide says "The prime directive of line-wrapping is: prefer to break at a higher syntactic level."

(Our width-limit is 100 characters.) This means that when faced with

  final Collection<FrameworkMethod> firstHalf = runner.subset(sortableMethods, firstHalfAnnotation);

breaking like this:

  final Collection<FrameworkMethod> firstHalf =
    runner.subset(sortableMethods, firstHalfAnnotation);

is better than

final Collection<FrameworkMethod> firstHalf = runner.subset(
    sortableMethods, firstHalfAnnotation);

This applies across a whole bunch of IntelliJ's line-breaking preferences. If the line can be broken into two by breaking parameters apart, or by putting all the parameters on a following line, or by breaking at an assignment, if it'll fit when broken at the highest level, that's the best. If it can only be fit onto two lines by breaking at the beginning of the parameter list, that's okay. If the only way to fit is to break inside a method call that's a parameter to the top-level call, then rearranging so the parts on successive lines are syntactically related.

Here's a harder example:

final ImmutableList<PushNotification> notifications = pushNotificationsHelperProvider.get().getNotifications(achieverGuid, grantedAchievements);

breaking between the arguments would be wrong, because there are higher level breaks available. Breaking after the equals, or after get(), or at the beginning of the method's argument list could all be defended.

I've looked, and haven't found a way to express this in IntelliJ's customization.

It's not just Google's style that recommends this: Java SE, javascript.

Please sign in to leave a comment.