Skip to content

Commit

Permalink
Add example and diagram to param order docs (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmans0n committed May 23, 2024
1 parent c0f1b2f commit 8f4fd30
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,31 @@ When writing Kotlin, it's a good practice to write the parameters for your metho

Modifiers occupy the first optional parameter slot to set a consistent expectation for developers that they can always provide a modifier as the final positional parameter to an element call for any given element's common case.

Additionally, if there is a `content` lambda, it should be used as a trailing lambda.

```mermaid
flowchart TD
A[Required params] --> B[Modifier]
B --> C[Optional parameters]
C --> D[Optionally a trailing lambda, like a content slot]
```

An example of the above diagram could be this:

```kotlin
//
@Composable
fun Avatar(
imageUrl: String, // Required parameters go first
name: String,
onClick: () -> Unit,
modifier: Modifier = Modifier, // Optional parameters, start with modifier
loadingContent: @Composable (() -> Unit)? = null, // Other optional parameters
errorContent: @Composable (() -> Unit)? = null,
content: @Composable () -> Unit, // A trailing mandatory lambda _can_ be last. Recommended placement for `content` slots.
) { ... }
```

More information: [Kotlin default arguments](https://kotlinlang.org/docs/functions.html#default-arguments), [Modifier docs](https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier) and [Elements accept and respect a Modifier parameter](https://github.com/androidx/androidx/blob/androidx-main/compose/docs/compose-api-guidelines.md#why-8).

Related rule: [compose:param-order-check](https://github.com/mrmans0n/compose-rules/blob/main/rules/common/src/main/kotlin/io/nlopez/compose/rules/ParameterOrder.kt)
Expand Down

0 comments on commit 8f4fd30

Please sign in to comment.