Skip to main content
2 of 10
added 288 characters in body
candied_orange
  • 119.7k
  • 27
  • 233
  • 369

What is vertical coupling? Do we care?

Formatting multiline statements is an arcane art that most auto formatters and style guides can't help you with. One habit I picked up is to ensure renaming things wont cause multiple lines to need to move due to what I thought was called vertical coupling. However, when I go searching for this term Google is no help.

Steve Jessop once took me to task over this issue.

Example code:

Widget widget = WidgetBuilder.build()
                             .color("green")
                             .sprockets(13);

Note the dots are vertically coupled and depend on the first line. Anything gets renamed in the first line and the rest must move to preserve the vertical coupling.

However, this could have been laid out like so:

Widget widget = WidgetBuilder
    .build()
    .color("green")
    .sprockets(13);

This avoids the need to reposition following lines after a rename.

This issue isn’t just about laying out builders. Here’s another example:

void MyClass::myFunction(const MyObject& obj,
                         const string& s1,
                         const string& s2,
                         const string& s3) {
    return;
}

Which could be laid out as:

void MyClass::myFunction(
        const MyObject& obj,
        const string& s1,
        const string& s2,
        const string& s3
) {
    return;
}

Does this concern have a better name?

candied_orange
  • 119.7k
  • 27
  • 233
  • 369