Suppose I’ve a very long method chaining
object.SetA(123).SetB(234).SetC(345) ...
what’s the best indentation? All compilers supports them?
object.
SetA(123).
SetB(234).
SetC(345) ...
or
object
.SetA(123)
.SetB(234)
.SetC(345) ...
Assuming that we’re talking about method chaining on the same object (each function retuns
*this), how about:Long lines aren’t just bad because your screen isn’t wide enough to contain them. If that’s all there was to it, just buy a bigger monitor or reduce the font size. They’re bad because people read code one line at a time, and if you’re doing too much in one line you break their tiny little carbon-based brains.
If all else fails, personally I’d indent it this way:
but I’d prefer:
Method chaining is a way to cram more onto a line, so if we’re then going to split the lines back out again, I don’t see the advantage. Sometimes we’re forced because
objectis actually a temporary (result of a function call rather than a named variable) or something and we can’t take a reference to it.You could perhaps just let the line run off the right-hand side. It’s annoying to have to scroll sideways or to have lines wrapping, but then again it’s already annoying to have to do this in one statement, so clearly there’s some incredibly important reason.