Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Learning Kotlin with detekt

inouehi
February 28, 2025

Learning Kotlin with detekt

『detektで学ぶKotlin』

Server-Side Kotlin LT大会 vol.14
2025/02/28 20:45~
https://server-side-kotlin-meetup.connpass.com/event/340992/

inouehi

February 28, 2025
Tweet

More Decks by inouehi

Other Decks in Programming

Transcript

  1. 2
 About Me
 • Hiroki Inoue
 • Software Engineer
 •

    Engineering Manager at Smartround Inc.

  2. 3
 About Me
 • Hiroki Inoue
 • Software Engineer
 •

    Engineering Manager at Smartround Inc.
 
 • 元々PHPer
 • 昨年10月から現職( Kotlinを使ってる会社) 
 • ロールはEM(本業で手を動かす機会は少なめ) 

  3. 9
 detekt is 何?
 • 静的解析ツール 
 • https://github.com/detekt/detekt
 •

    https://detekt.dev/
 つまり
 Kotlinに詳しいのでは? 
 (期待) 

  4. 10
 detekt is 何?
 • 静的解析ツール 
 • https://github.com/detekt/detekt
 •

    https://detekt.dev/
 ならば
 Kotlinを教われるのではないか 
 (仮説) 

  5. 16
 学びの経路 
 1. detektのルール
 ◦ Kotlinはこう書くべしというベストプラクティス(のひとつ) 
 2. detektそのもの


    ◦ Kotlinで書かれた運用実績のあるプロダクションコード 
 3. issue
 ◦ Kotlinの言語仕様や書き方に関する話題 

  6. 23
 platform type
 "platform types — types which the Kotlin

    compiler uses, when interoperating with code written for another platform (e.g., Java)."
 
 https://kotlinlang.org/spec/type-system.html#platform-types

  7. 27
 このissueが示唆すること 
 • UnnecessaryInnerClassはouter classのメンバにアクセスしないinner classを 指摘するルール
 ⇒outerにアクセスすることがinner classの個性


    • しかし兄弟classにアクセスしてても指摘するというfalse positiveがある
 ⇒innerは、outerだけじゃなく outerの別のinnerにもアクセスできる 

  8. 32
 it
 “It's very common for a lambda expression to

    have only one parameter.
 
 If the compiler can parse the signature without any parameters, the parameter does not need to be declared and -> can be omitted. The parameter will be implicitly declared under the name it:”
 
 https://kotlinlang.org/docs/lambdas.html#it-implicit-name-of-a-single-p arameter

  9. 40
 コードを改修するまでの流れ 
 1. ラベルでフィルターして Issuesを読み漁る 
 2. 仕様を理解する 


    ◦ コードを読む 
 ◦ 実行する
 ◦ テストを読む 
 3. 開発流儀を理解する 
 ◦ Contribution Guideを読む
 ◦ マージ済みの PRを読む
 4. 開発に着手する 
 結果として見送った 
 issueからも学べる
  10. 41
 Contribution Guide
 • commitメッセージの流儀 
 • ユニットテストの実行方法 
 •

    新しいルールの作り方 
 • ルール説明の書き方 
 • エラー検出時のメッセージの書き方 
 etc.

  11. 43
 ルールを追加した時に触れたもの 
 • ルール
 • テスト
 • detekt.yml[1]
 •

    default-detekt-config.yml
 • RuleSetProvider[2]
 1. ルールの有効化・無効化の指定やパラメータを渡すことができる。 
 2. ルールにはカテゴリがあり、カテゴリ毎に RuleSetProviderがある。

  12. 45
 type resolution
 • type resolution[1]が有効でないと実行されないルールがある 
 • `--debug`オプションで確認できる 


    1. https://github.com/detekt/detekt/blob/main/website/docs/gettingstarted/type-resolution.md
 The rule 'AvoidReferentialEquality' requires type resolution but it was run without it.
  13. 46
 type resolution
 • type resolution[1]が有効でないと実行されないルールがある 
 • `--debug`オプションで確認できる 


    
 
 • ./gradlew detektMain などして有効化できる 
 • 手元ではmavenLocalで試せる
 1. https://github.com/detekt/detekt/blob/main/website/docs/gettingstarted/type-resolution.md
 The rule 'AvoidReferentialEquality' requires type resolution but it was run without it.