Ktlint

Throwgether
TakoDigital
Published in
3 min readJun 7, 2023

การทำ Automate Code Review หรือ การทำ Tech Standard เราต้องหา Code Style สักอันมาเป็นที่ยึดเหนี่ยวจิตใจ ในบทความนี้พี่แมวก็จะมาสอนให้ทุกคนรู้จักกับ lint (ลิ้น) และสอนให้ทุกคนใช้งาน lint (ลิ้น) ได้อย่างคล่องแคล่ว !!!

เริ่มจาก lint (ลิ้น) คืออะไร?
lint เป็น Static code analysis tool ตัวหนึ่ง โดย lint จะมีในทุก ๆ ภาษาโปรแกรม
ktlint จึงหมายถึง เครื่องมือในการช่วยตรวจสอบ Format โค้ดของภาษา Kotlin

เกิดจากการอ้างอิง Code Style 2 ตัว คือ
1.Android Kotlin Style Guide
2.kotlinlang.org

https://pinterest.github.io/ktlint/0.49.1/

โดยที่ lint จะช่วยตรวจสอบ Code Style ให้ตรงแบบฉบับของ สามารถดูได้ที่ลิ้งนี้

https://pinterest.github.io/ktlint/0.49.1/rules/standard/

วิธีการติดตั้งรูปแบบต่างๆ

  1. สามารถติดตั้งโดยใช้ Gradle Plugin ที่มีคนทำไว้ เช่นของ JLLeitschuh ที่เป็นที่นิยม
    >>> https://github.com/jlleitschuh/ktlint-gradle <<<

จะทำให้ได้ ktlint อยู่บน project ของเรา

buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jlleitschuh.gradle:ktlint-gradle:<current_version>"
}
}

apply plugin: "org.jlleitschuh.gradle.ktlint"

version lasted : 11.3.2 จะพบปัญหานี้(ในบางโปรเจ็ค)ที่ gradle version ต่ำกว่า 6.8

ขยับไปใช้ JLLeitschuh ที่ version: 9.2.1 แทน

ตัวอย่างคำสั่ง

./gradlew ktlintCheck
./gradlew ktlintFormat
💡 noted : To fix the error that cannot be fixed automatically, you have to fix it manually.

ข้อดี : ใช้เป็นมาตรฐานของ Project

ข้อเสีย : version conflict , รัน ktlintFormat แล้ว error บ่อยครั้ง

2. ติดตั้งผ่าน https://pinterest.github.io/ktlint/0.49.0/ โดยตรง และ ป้อนคำสั่งที่ terminal

จะทำให้ได้ ktlint อยู่บนเครื่องของเรา

brew install ktlint

ktlint --help
ktlint --format
ผลลัพธ์จากการใช้งาน lint และได้รับการแจ้งเตือน
ตัวอย่างคำสั่ง (เยอะมาก)
ตัวอย่างหลังจาก format

ข้อดี : ทำงานได้ดีที่สุด ปรับ config ได้ละเอียด

ข้อเสีย : learning curve (ถ้าอยากใช้งานได้ดี ต้องศึกษา command และ config ต่างๆ)

3. หรือใช้ Android Studio Plugin

จะทำให้ได้ ktlint อยู่บน Android Studio ของเรา

  • Preference > Plugins > Marketplace > search “ktlint” >install

ข้อดี : ง่าย แจ้งเตือน ปรับแก้ทีละส่วนโดยไม่เกิดผลกระทบ

ข้อเสีย : ยังไม่อัพเดทเวอชั่น หลอน(บางที installed แต่ก็ใช้งานไม่ได้ ต้อง reset ใหม่)

ปํญหาที่พี่แมวเจอ
trailing commas

trailing commas

เพราะ lint จะมีการเติม , ซึ่งมีคุณสมบัติที่ไม่ตรงกับ kotlin ที่เวอชั่น ต่ำกว่า 1.4.0

เบื้องต้น ทำให้การเพิ่มไฟล์ .editorconfig เพื่อลดอาการบาดเจ็บ (https://editorconfig.org/)

สรุป

พี่แมวว่า ktlint เป็นหนึ่งใน Code style ที่น่าใช้ เหมาะกับนำมาใช้กับ Android project

เพื่อให้การ Review นั้นง่ายขึ้นและในอนาคตยังสามารถประยุกต์ใช้กับ project อื่นๆที่เขียนด้วย kotlin ได้

จากบทความนี้พี่แมวจะขอสรุปวิธีดำเนินการของพี่แมวที่จะใช้ลิ้น(lint) กับ project ที่มี kotlin , gradle เวอชั่นต่ำมากๆ

  1. ติดตั้งวิธีที่ 2
  2. เพิ่มไฟล์ .editorconfig เพื่อแก้ปัญหา trailing commas
  3. ให้ทำการ format code ทั้งหมดใน project ด้วย ktlint (วิธีที่ 2)
  4. ต่อไปเมื่อพี่แมวจะทำการอัพเดท code ที่ก็จะ format code ด้วย ktlint เสมอๆ
  5. ติดตั้งวิธีที่ 3 เพื่อเป็นตัวช่วยให้สังเกตุได้ง่ายขึ้นเวลาเขียน code inline
    (มันจะแดงเทือกเลย)
  6. ทำการอัพ gradle version และ kotlin version ให้ล่าสุด
  7. ติดตั้งวิธีที่ 1 ลงใน project เพื่อเป็น tech standard ต่อไปป

ref

https://pinterest.github.io/ktlint/0.49.1/ https://github.com/jlleitschuh/ktlint-gradle https://blog.mindorks.com/code-formatting-in-kotlin-using-ktlint/ https://medium.com/20scoops-cnx/มาทำ-automate-code-review-กัน-8b483e29ed69

--

--

Throwgether
TakoDigital

เมื่อเด็กเกาะเบาะต้องลงมือ Dev เอง