From 6c4026ad1affd93c9efb72ea73f0d308c57921f8 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 30 Sep 2024 11:23:04 -0500 Subject: [PATCH 1/3] Recommend single indent, not double indent Fixes #215 --- functions.Rmd | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/functions.Rmd b/functions.Rmd index 50e2d42..338cbf4 100755 --- a/functions.Rmd +++ b/functions.Rmd @@ -28,20 +28,23 @@ There are two options if the function name and definition can't fit on a single } ``` -* Double-indent: Place each argument of its own **double** indented line. + The trailing `)` and leading `{` go on the same line as the last argument. + +* Single-indent: Place each argument of its own **single** indented line. ```{r, eval = FALSE} long_function_name <- function( - a = "a long argument", - b = "another argument", - c = "another long argument") { + a = "a long argument", + b = "another argument", + c = "another long argument" + ) { # As usual code is indented by two spaces. } ``` -In both cases the trailing `)` and leading `{` should go on the same line as the last argument. + The trailing `)` and leading `{` go on a new line. -Prefer function-indent style to double-indent style when it fits. +Prefer function-indent style to single-indent style when it fits. These styles are designed to clearly separate the function definition from its body. From 7bbacee64f2575999f3677d7b14a166cfd8c6f6b Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 1 Oct 2024 17:42:09 -0500 Subject: [PATCH 2/3] Respond to feedback; add S7 content --- functions.Rmd | 64 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/functions.Rmd b/functions.Rmd index 338cbf4..03e27c2 100755 --- a/functions.Rmd +++ b/functions.Rmd @@ -14,25 +14,15 @@ row_adder() permutation() ``` -## Long lines +## Multi-line function defintions -There are two options if the function name and definition can't fit on a single line: +There are two options if the function name and definition can't fit on a single line. In both cases, each argument goes on its own; the difference is how deep you indent it and where you put `)` and `{`: -* Function-indent: place each argument on its own line, and indent to match the opening `(` of `function`: - - ```{r, eval = FALSE} - long_function_name <- function(a = "a long argument", - b = "another argument", - c = "another long argument") { - # As usual code is indented by two spaces. - } - ``` - - The trailing `)` and leading `{` go on the same line as the last argument. - -* Single-indent: Place each argument of its own **single** indented line. +* **Single-indent**: indent the argument name with a single indent (i.e. two spaces). + The trailing `)` and leading `{` go on a new line. ```{r, eval = FALSE} + # Good long_function_name <- function( a = "a long argument", b = "another argument", @@ -42,9 +32,17 @@ There are two options if the function name and definition can't fit on a single } ``` - The trailing `)` and leading `{` go on a new line. +* **Function-indent**: indent the argument name to match the opening `(` of `function`. + The trailing `)` and leading `{` go on the same line as the last argument. -Prefer function-indent style to single-indent style when it fits. + ```{r, eval = FALSE} + # Bad + long_function_name <- function(a = "a long argument", + b = "another argument", + c = "another long argument") { + # As usual code is indented by two spaces. + } + ``` These styles are designed to clearly separate the function definition from its body. @@ -60,6 +58,38 @@ long_function_name <- function(a = "a long argument", If a function argument can't fit on a single line, this is a sign you should rework the argument to keep it [short and sweet](https://design.tidyverse.org/defaults-short-and-sweet.html). +## S7 + +In S7, the method definition can be long because the function name is replaced by a method call that specifies the generic and dispatch classes. In this case we recommend the single-indent style. + +```{r, eval = FALSE} +method(from_provider, list(openai_provider, class_any)) <- function( + provider, + x, + ..., + error_call = caller_env() +) { + ... +} +``` + +If the method definition is too long to fit on one line, use the usual rules to +spread the method arguments across multiple lines: + +```{r, eval = FALSE} +method( + from_provider, + list(openai_provider, class_any, a_very_long_class_name) +) <- function( + provider, + x, + ..., + error_call = caller_env() +) { + ... +} +``` + ## `return()` Only use `return()` for early returns. Otherwise, rely on R to return the result From 4aac7aa1702e4f971ea7d8132fb46b8a9a094973 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 2 Oct 2024 07:29:43 -0500 Subject: [PATCH 3/3] Feedback from review --- functions.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/functions.Rmd b/functions.Rmd index ceeee2f..2d96667 100755 --- a/functions.Rmd +++ b/functions.Rmd @@ -34,7 +34,7 @@ map(xs, ~ mean((.x + 5)^2)) ## Multi-line function defintions -There are two options if the function name and definition can't fit on a single line. In both cases, each argument goes on its own; the difference is how deep you indent it and where you put `)` and `{`: +There are two options if the function name and definition can't fit on a single line. In both cases, each argument goes on its own line; the difference is how deep you indent it and where you put `)` and `{`: * **Single-indent**: indent the argument name with a single indent (i.e. two spaces). The trailing `)` and leading `{` go on a new line. @@ -50,11 +50,11 @@ There are two options if the function name and definition can't fit on a single } ``` -* **Function-indent**: indent the argument name to match the opening `(` of `function`. +* **Hanging-indent**: indent the argument name to match the opening `(` of `function`. The trailing `)` and leading `{` go on the same line as the last argument. ```{r, eval = FALSE} - # Bad + # Good long_function_name <- function(a = "a long argument", b = "another argument", c = "another long argument") {