Why you should care

Whenever null seems like a good idea, use Option instead.

As far as types are concerned, null is a bit of a lie:

val s: String = null

The compiler believes s to be a String and will accept it wherever one is required. The compiler is, obviously, wrong:

// java.lang.NullPointerException
// 	at repl.Session$App$$anonfun$2.apply(avoid_null.md:15)
// 	at repl.Session$App$$anonfun$2.apply(avoid_null.md:15)

Whenever you’re using null, you’re hindering the compiler’s ability to prove your code incorrect.

How we detect

This Code Insight counts one occurrence each time null is used (strings are not concerned):


Non-compliant Code Example

object HelloWorld {
	def concat(a: String, b: String): String = {
		if(a == null)      b     // +1
		else if(b == null) a    // +1
		else               s"$a$b"

Compliant Solution

object HelloWorld {
	def concat(a: Option[String], b: Option[String]): String =



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.