What is the expected indentation for chained method calls? #11320
Replies: 4 comments 1 reply
-
Whatever looks nice. Many people have different ideas on what the perfect indention is. We just need to accept anything that looks acceptable and flag the ones that are completely bad. Sometimes we look at what IDEs provide as the default formatting. Since we are talking about implementation, we should be looking at the existing issues reported and see what has been provided. Also look at what all the IDEs do. |
Beta Was this translation helpful? Give feedback.
-
I tried a few things, I am sharing what I propose as the final indentation- obj.asd( // indent:x exp:x
).asd( // indent:x+4 exp:x+4
).asd( // indent:x+4 exp:x+4
arg1 // indent:x+8 exp:x+8
); // indent:x+4 exp:x+4 obj.asd() // indent:x exp:x
.asd() // indent:x+4 exp:x+4
.asd(arg1, arg2, // indent:x+4 exp:x+4
arg3 // indent:x+8 exp:x+8
) // indent:x+4 exp:x+4
.asd( // indent:x+4 exp:x+4
); // indent:x+4 exp:x+4 obj.asd( // indent:x exp:x
); // indent:x exp:x I am completely open to suggestions. |
Beta Was this translation helpful? Give feedback.
-
I would consider all that cases as bad (my style). as used define location of
I would focus less on indentation (see simple rule above that might be useful, but better to not think on this to avoid overlaps/conflicts) and focus more on location of chained methods Should be good to my mind (indentation excluded intentionally, to focus on what matter the most):
as you can see first and last line can be chaned metods on same line and only user knows where to wrap, but mostlikely we can presume that all methods in between is good for being on new line. |
Beta Was this translation helpful? Give feedback.
-
@romani Um this is not related to obj.asd( // indent:x exp:x
).asda( // indent:x exp:x
) // indent:x+4 exp:x+4
.asd().asd( // indent:x+4 exp:x+4
); // indent:x+4 exp:x+4 The second line's indentation is peculiar IMO, shouldn't it be obj.asd( // indent:x exp:x
).asda( // indent:x+4 exp:x+4
) // indent:x+4 exp:x+4
.asd().asd( // indent:x+4 exp:x+4
); // indent:x+4 exp:x+4 I have made good progress in |
Beta Was this translation helpful? Give feedback.
-
Just a general discussion, consider a few examples-
Example-1
Example-2 (4 represents
lineWrappingIndentation
)Example-3
Example-4
The main confusion is when
(
and)
are on different lines, is there any style guide that checkstyle follows for indentation which mentions such cases, I wasn't able to find much information in https://google.github.io/styleguide/javaguide.html#s4.1-braces, https://www.oracle.com/technetwork/java/codeconventions-150003.pdfAlso please share your personal opinion on this, let's finalize and implement this.
@romani @strkkk @nmancus1 @rnveach @pbludov
Beta Was this translation helpful? Give feedback.
All reactions