Skip to content

Files

Latest commit

 

History

History
29 lines (20 loc) · 624 Bytes

VarCouldBeVal.md

File metadata and controls

29 lines (20 loc) · 624 Bytes

Pattern: Use of var instead of val

Issue: -

Description

Reports var declarations (locally-scoped variables) that could be val, as they are not re-assigned. val declarations are assign-once (read-only), which makes understanding the current state easier.

Example of incorrect code:

fun example() {
    var i = 1 // violation: this variable is never re-assigned
    val j = i + 1
}

Example of correct code:

fun example() {
    val i = 1
    val j = i + 1
}

Further Reading