Avoid explicit type when it can be inferred from literal value affected

Why you should care

For code readability purpose, if a property initializer is a scalar value or the return type can be clearly inferred from the body then it can be omitted.

How we detect

CAST Highlight counts one occurrence each time a variable (or class property) is declared with an explicit type and initialized with one of the following scalar values:

  • “…” or ‘…’ (string)
  • false or true (boolean)
  • integer or signed integer
  • decimal : a dot followed by a digit number

Bad Code

var a : String = "it's a string" // +1 VIOLATION
val b : Boolean = false // +1 VIOLATION
const val c : Int = 123 // +1 VIOLATION
val d : Any = -123 // +1 VIOLATION
val e : Float = .123 // +1 VIOLATION



About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.