SaFi Bank Space : Executing Code Validation Locally

Each time when we created a PR, GitHub will execute some code validations for our codes, including ktlint and unit tests. Although it will be executed online after created PR, we strongly recommend run this validation locally. The first reason for doing that is running locally is way faster that GitHub Actions, the second one is you don’t need to commit time by time and make git log more complex.

Command for running code validation

./gradlew build -x integrationTest --info

It’s the excally command when we executing code validation on GitHub Actions.

If any ktlint check cannot passed or testing failed, it will give you the check result with detail. 

Writing perfect code

For executing ktlint check while writing code, Ktlint(unofficial) plugin for IDEA is the best solution. It will mark unproper codes with underscore and give you warnings even mark as error level by default.

 

Minor configurations for Ktlint plugin

Trailing comma

Ktlint(unofficial) plugin decide trailing comma as an error, but the  coding conventions (https://kotlinlang.org/docs/coding-conventions.html) are suggested to use trailing comma. Depends on that we need to disable the rule of ktlint plugin by adding trailing-comma to disabled rules.

Multiline if else

The other issue with ktlint check of our self is multiline-if-else, Ktlint plugin also define it as error, and our code validation will not check that, you may disable this check by add multiline-if-else to disabled rules.

Example of ktlint configuration

Format file with Ktlint(unofficial) is not suggested! 

Ktlint(unofficial) plugin has a beneficial functionality: format file with ktlint, it can format our file automatically. But don’t do that except the whole file is created by you and it’s haven’t been committed yet, otherwise it may affect more code changes and make code reviewer too confuse to review.

Attachments: