开发者

indentation for method chaining [closed]

开发者 https://www.devze.com 2023-04-01 04:18 出处:网络
As it currently stands, this question is not a good fit for our Q&A开发者_如何学Go format. We expect answers to be supported by facts, references,or expertise, but this question will likely so
As it currently stands, this question is not a good fit for our Q&A开发者_如何学Go format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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) ...


IMO, the second one is better. By starting a line with a ., it makes it explicit that it's a continuation of a chained call.


Yes, all compilers will support them. All you are doing is adding white space which is removed by the compiler during lexical analysis.

This is a question of personal preference but I would agree with the point made by Mike Kwan.


I would write this if the chain is too long, and cannot come on a single line:

object.SetA(123)
      .SetB(234)
      .SetC(345)
      .SetD(345)
      .SetE(345)
      .SetF(345);

Otherwise I would go for this:

object.SetA(123).SetB(234).SetC(345).SetD(345).SetE(345).SetF(345);


Assuming that we're talking about method chaining on the same object (each function retuns *this), how about:

object.setA().setB().setC();
object.setD().setE().setF();

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:

object
    .setA()
    .setB()
    ...
;

but I'd prefer:

object.setA();
object.setB();
...

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 object is 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.


C++ isn't whitespace sensitive, all of them would work on any C++ compilers (well... assuming they have no bug in the lexer). I prefer the last one anyway, with terminating semicolon placed on its own line (that makes adding more chain easier, a simple line copy would do it).

0

精彩评论

暂无评论...
验证码 换一张
取 消