Back to all reviewers

avoid null in Scala

apache/spark
Based on 4 comments
Other

Avoid using null values in Scala code and prefer Option types for representing optional values. Null usage can lead to NullPointerException at runtime and makes code less safe and predictable.

Null Handling Other

Reviewer Prompt

Avoid using null values in Scala code and prefer Option types for representing optional values. Null usage can lead to NullPointerException at runtime and makes code less safe and predictable.

When working with optional values, use Option[T] and handle the None case explicitly. For Java interoperability where null is required, use .orNull on the Option to convert safely:

// Avoid this
val aliasName = if (rightSideRequiredColumnNames.contains(name)) {
  generateJoinOutputAlias(name)
} else {
  null  // Dangerous - can cause NPE
}

// Prefer this
val aliasName = if (rightSideRequiredColumnNames.contains(name)) {
  Some(generateJoinOutputAlias(name))
} else {
  None
}

// When calling Java functions that expect null
javaFunction(aliasName.orNull)

This pattern is especially important when dealing with serializable classes where transient fields can become null after deserialization, potentially causing unexpected NPEs. Make fields private when they could be null due to serialization to prevent accidental access to null values.

4
Comments Analyzed
Other
Primary Language
Null Handling
Category

Source Discussions