Skip to content

Add newline after multiline function declaration #371

Open

Description

@krlmlr
# Good
f1 <- function(a, b) {
  code
}

f2 <- function(a, b,
               c) {

  code
}

# Bad
f3 <- function() {

  code
}

Activity

  1. lorenzwalthert commented on Mar 13, 2018

    @lorenzwalthert
    Collaborator

    Is there a corresponding rule in the tidyverse style or do you plan to add one?

  2. krlmlr commented on Mar 13, 2018

    @krlmlr
    MemberAuthor

    I remember Hadley suggested that an empty line after a long function header helps readability. I wouldn't formalize this as a rule yet, but I've seen this pattern a few times in dplyr.

  3. lorenzwalthert commented on Mar 13, 2018

    @lorenzwalthert
    Collaborator

    Ok. I think this is easy to implement. I just think for the sake of consistency, we should in general aim to keep the style guide in sync with styler.

  4. lorenzwalthert commented on Mar 13, 2018

    @lorenzwalthert
    Collaborator

    For a propper implementation, we'd need #357 because we want to add a line break dependent on whether a token is multi-line, which we cannot do if the multi-line attribute is not up-to date. The multi-line attribute is not up-to-date because it is only updated after all line break rules were applied.

  5. lorenzwalthert commented on Mar 13, 2018

    @lorenzwalthert
    Collaborator

    Blocked by #357.

  6. lorenzwalthert commented on Jan 26, 2019

    @lorenzwalthert
    Collaborator

    Implemented in mlr branch: lorenzwalthert@2a3fbc6

  7. lorenzwalthert commented on Apr 17, 2019

    @lorenzwalthert
    Collaborator

    The implementation in the mlr branch now depends on the number of lines in the body.

  8. lorenzwalthert commented on Apr 17, 2019

    @lorenzwalthert
    Collaborator

    For a propper implementation, we'd need #357 because we want to add a line break dependent on whether a token is multi-line, which we cannot do if the multi-line attribute is not up-to date. The multi-line attribute is not up-to-date because it is only updated after all line break rules were applied.

    Wrong. We need the exact line count, which we can compute ad-hoc within the transformer. It's as simple as in lorenzwalthert@d9f1b22. Performance implications probably small for non-package code because only taking effect for nests that are function declarations. Implementing it otherwise is most likely not much faster (i.e. doing it with a visitor for all top-level tokens) because most function declarations are not nested. (apparently there are more nested cases where this is insufficient, see #773). This rule needs to be at the end of the line-break transformers because it depends on them.

  9. maelle commented on Oct 14, 2021

    @maelle
    Contributor

    @krlmlr any specific examples of this pattern? 🙏

  10. maelle commented on Oct 14, 2021

    @maelle
    Contributor

    For context I was looking into never removing the newline at the beginning of a function body if there was one, following a comment by @mpadge. 😁

  11. krlmlr commented on Oct 14, 2021

    @krlmlr
    MemberAuthor

    I have tweaked the "bad" example in the original post.

    Keeping an extra newline, at least in non-strict mode, doesn't sound too bad as a starter.

  12. maelle commented on Oct 14, 2021

    @maelle
    Contributor

    I have tweaked the "bad" example in the original post.

    That's what I had guessed it should be. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions