Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put the dot with fn name when chaining #230

Merged
merged 1 commit into from
Jan 30, 2015

Conversation

justjake
Copy link
Collaborator

Reasons:

  • styleguide already uses leading-dot style.
  • leading dot emphasizes that the line is a method call, not a new statement. Consider the readability without whitespace:
foo.
something().
else(); // <-- github JS parser error!

foo
.something()
.else();
  • to spite @horaceko
  • no one in semicolon-terminated languages would ever use the trailing-dot style.

@iamnirav
Copy link
Contributor

👍 because I like this PR but also to spite @horaceko

@horaceko
Copy link
Collaborator

👎 to this PR but 👍 to spite @horaceko

In all seriousness, I'm actually largely indifferent, and I defer to the opinion of the masses.

@nomcopter
Copy link

👍

@hshoff
Copy link
Member

hshoff commented Jan 24, 2015

@justjake could you change Put the dot with the function name. to Leading dot emphasizes that the line is a method call, not a new statement.?

@justjake
Copy link
Collaborator Author

@hshoff k

hshoff added a commit that referenced this pull request Jan 30, 2015
put the dot with fn name when chaining
@hshoff hshoff merged commit 7a9ca5a into airbnb:master Jan 30, 2015
@hshoff
Copy link
Member

hshoff commented Jan 30, 2015

🍻

@justjake justjake deleted the no-trailing-dot branch January 30, 2015 21:53
@rafaelsales
Copy link

rafaelsales commented Aug 23, 2016

I think both options should be valid. Ruby style guide consider both ok:

https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains

Adopt a consistent multi-line method chaining style. There are two popular styles in the Ruby community, both of which are considered good—leading . (Option A) and trailing . (Option B).

(Option A) When continuing a chained method invocation on another line keep the . on the second line.

bad - need to consult first line to understand second line

one.two.three.
  four

good - it's immediately clear what's going on the second line

one.two.three
  .four

(Option B) When continuing a chained method invocation on another line, include the . on the first line to indicate that the expression continues.

bad - need to read ahead to the second line to know that the chain continues

one.two.three
  .four

good - it's immediately clear that the expression continues beyond the first line

one.two.three.
  four

A discussion on the merits of both alternative styles can be found here.

Two reasons I like using dot in the first line of each line:

  1. When pasting code, it works, because on a new line the REPL knows the sentence did not end yet. You can reproduce that by pasting the following code in node:

    const foo = { something: function() { return { else: function () {} } } }
    foo.
    something().
    else()

    You try the example above following the current style guide, it will fail with: Invalid REPL keyword

  2. When indentation is wrong, it's easier to see the sentence continues in the next line.

@ljharb
Copy link
Collaborator

ljharb commented Aug 23, 2016

@rafaelsales idiomatic ruby styles can be problematic for a number of reasons, so that's not a point in its favor.

Our guide requires either a) everything fits on one line, or b) each thing goes on its own line.

In other words,

one.two.three
  .four

is invalid - it must either be one.two.three.four or:

one
  .two
  .three
  .four

In addition, our guide and linter rules require the dot to begin the next line - we never permit the dot to trail on the end.

@rafaelsales
Copy link

rafaelsales commented Aug 23, 2016

You could have summarized your answer in one line: "Our rule is the one you can find in the readme"

I was trying to start a discussion here showing the other style also has advantages over the current one, thus shouldn't be considered "bad" as the README says. Instead it should be offered as just another option that certain teams can adopt and still be able to say "we follow airbnb style guide"

@ljharb
Copy link
Collaborator

ljharb commented Aug 23, 2016

Fair enough :-)

I don't think that the other style has advantages, and I highly dislike it in Ruby as well. As far as I'm aware, the only reason that style became popular in Ruby is because some old version of Ruby or earlier (i forget which) didn't support the "leading dots" style. Now that Ruby supports it, I think that's the style that should be used in both JS and Ruby.

As to your specific reasons:

  1. REPL use cases are not common, and should not be optimized for. That might be a nice perk of "trailing dots", but it's not something that overrides the readability of "leading dots".
  2. The linter catches when indentation is wrong, so there's no need to inflict that burden on humans.

If someone wants to be able to say "we follow the airbnb style guide", they have to cleave to our rules - and that includes using "leading dots".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants