Improving Code Quality - Session 47: Breach of non-performance
Key point
Setting a value with special meaning as a default parameter can cause bugs through unintended object creation.
Details
This covers the risks that can occur when implementing a Null Object using Kotlin's default parameter.
Setting a value with special meaning, such as id = 0, as a default parameter creates the risk that an 'invalid object' may be unintentionally created when a developer mistakenly omits a specific argument. This problem arises because the default value represents a special state rather than a general value.
Alternatives for a safer design are as follows:
- Define an explicit instance such as
INVALIDthrough a companion object. - Clearly distinguish between
ValidandInvalidusing a sum type leveraging a sealed interface. - Use a nullable type to represent the invalid state with
null.
In conclusion, default parameters should use general values that anyone can predict, rather than values with a special purpose.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.