
detekt に指摘されながら、ドキュメントを読む。
【detekt / ktlint】元 Twitter 管理者 @mrmans0n らによって Jetpack Compose Rules フォークが進化している件 #プログラミング #kmmhttps://t.co/ToXFLbyXcR
— chanzmao (@maochanz) October 23, 2023
こんなコードがあったとして。
Column {
InnerContent()
}
@Composable
private fun InnerContent() {
Text(...)
Image(...)
Button(...)
}
以下、知らなかった流儀。
🧩 Composable 関数はレイアウトを一つだけ出力する
A composable function should emit either 0 or 1 pieces of layout, but no more. A composable function should be cohesive, and not rely on what function it is called from.
コンポーザブル関数は、レイアウトのピースを0個または1個だけ発行するべきで、それ以上は発行してはいけません。コンポーザブル関数は結束性を持ち、呼び出される関数に依存すべきではありません。
レイアウトのネストのコストはあまり気にしなくて良い、とのこと。
Nesting of layouts has a drastically lower cost vs the view system, so developers should not try to minimize UI layers at the cost of correctness.
レイアウトのネストには、ビューシステムに比べてはるかに低いコストがかかるため、UIの階層を正確性の犠牲にして最小化しようとしないべきです。
👉 Do not emit multiple pieces of content - Twitter Jetpack Compose Rules
ということで、一見不要に見える Column を追加する。
@Composable
private fun InnerContent() {
Column {
Text(...)
Image(...)
Button(...)
}
}
🧩 デフォルトを持つパラメータ modifier: Nodifier = Modifier は必須
They are especially important for your public components, as they allow callers to customize the component to their wishes.
特に、公開コンポーネントにとって Modifier は非常に重要であり、呼び出し元がコンポーネントを希望に合わせてカスタマイズできるようにします。
👉 When should I expose modifier parameters? - Jetpack Compose Rules
Composables that accept a Modifier as a parameter to be applied to the whole component represented by the composable function should name the parameter modifier and assign the parameter a default value of Modifier.
Composable 関数内でコンポーザブル関数を表すコンポーネント全体に適用するための修飾子をパラメータとして受け入れる場合、そのパラメータは "modifier" という名前を付け、パラメータに Modifier のデフォルト値を割り当てるべきです。
👉 Modifiers should have default parameters - Jetpack Compose Rules
ということで、親からの Modifier を受け入れるようにします。デフォルト値もつけておきます。
@Composable
private fun InnerContent(modifier: Modifier = Modifier) {
Column {
Text(...)
Image(...)
Button(...)
}
}
🧩 受け取った Modifier パラメータは最上位のレイアウトにのみ適用する
Modifiers should be applied once as a first modifier in the chain to the root-most layout in the component implementation. Since modifiers aim to modify the external behaviors and appearance of the component, they must be applied to the top-most layout and be the first modifiers in the hierarchy. It is allowed to chain other modifiers to the modifier passed as a param if needed.
Modifier は、コンポーネントの実装内でルートのレイアウトに最初の Modifier として一度適用すべきです。Modifier はコンポーネントの外部の動作や外観を変更することを目的としているため、最上位のレイアウトに適用し、階層内で最初の Modifier である必要があります。必要に応じて、パラメータとして渡された Modifier に他の Modifier を連鎖させることは許可されています。
👉 Modifiers should be used at the top-most layout of the component - Jetpack Compose Rules
ということで、最上位ルートの Column で適用します。
@Composable
private fun InnerContent(modifier: Modifier = Modifier) {
Column(modifier = modifier) {
Text(...)
Image(...)
Button(...)
}
}
🧩 まとめ
@Composable 内のレイアウトに関する Modifier は、親 (呼び出し元) から持ってきて、最上位のレイアウトで一度だけ適用する。
detekt を使うことで、なんとなく記述していた部分がスッキリしてきます。

![✔[Show logcat automatically]
✔[Clear log before launch]](https://i0.wp.com/android.benigumo.com/wp-content/uploads/2023/10/51ec1c8bc54284c8dde09ff694e855e7.png?ssl=1)
![2023年8月31日以降 アプリは Android 13(API レベル 33)以降を対象にする必要があります。
2023年9月26日
Jetpack Compose Dependency versions API-33 vs API-34
2023年9月23日
メモリーリークを防ぐには欠かせない Lifecycle Observer は remove/unregister 不要
2023年9月10日
Gson が R8 で落ちる
2023年8月28日
【Kotlin DSL】Deprecated な packagingOptions が fun Packaging.() に変換される件
2023年8月27日
【Android】通知が表示されないときのアプリ別の端末設定 ON/OFF ⚙️
2023年8月25日
Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /app/build/outputs/mapping/debug/missing_rules.txt
2023年8月24日
Unresolved reference: BuildConfig
2023年8月16日
ログインに使っている Twitter (X) アカウントの連携を削除する手順
2023年8月11日
【iPhone】Yahoo天気アプリ 「現在地ボタンが変わりました」は「現在地ボタンは無くなりました」では?
2023年8月10日
【Mac】絵文字 や GitHubショートコード は「GitHub Emoji Picker」で入力する 😀
2023年8月9日
ChatGPT クローラー の UA(ユーザーエージェント) と IPアドレス範囲 まとめ
2023年8月4日
Android OS version market share
2023年7月30日
【iPhone】「パケ詰まり」「パケ止まり」を修復するショートカット 📱
2023年7月26日
Android Studio Giraffe | 2022.3.1 の dependencies の揃え方
2023年7月24日
将来、URL の twitter.com は x.com となるんだろうか。
2023年7月20日
Service Locator と DI どちらを使うか、という話。
2023年7月13日
【iPhone/Android】富士山 にいくなら「富士山」アプリ 🏔
2023年7月12日
Chromecast の 素晴らしい背景画面 をダウンロードできる件
2023年7月11日
スマホスタンド(充電不可)を作ってみたが、まあまあ使っている 📱
2023年7月10日
【Android Studio】[File] - [Project Structure] - [Suggestions] で「Update」する dependencies の妥当性 💉
2023年7月7日
【WordPress】自分のブログで「無効なトラフィック」を発生させないための Adsense タグ記述
【Jetpack Compose】TextField の フォーカス と IME 開閉 と カーソル位置](https://i0.wp.com/android.benigumo.com/wp-content/uploads/2023/10/output.gif?ssl=1)