Junior — Middle
What methods of checking and handling missing or uninitialized data do you know in programming?
sobes.tech AI
Answer from AI
In programming, the following approaches are often used to check and handle missing or uninitialized data:
- Null or nil check — the most common method, where you check if a variable is not equal to null before using it.
- Using Optional (e.g., in Kotlin, Swift) — wrapping the value in a special type that explicitly indicates the possibility of missing data, requiring explicit extraction of the value.
- Default values — if data is missing, a predefined value is substituted to avoid errors.
- Exceptions and error handling — attempting to access uninitialized data throws an exception, which is then handled.
- Checking with methods like isEmpty(), isBlank() — for strings or collections, checking that they are not empty.
Example in Kotlin with Optional and null check:
var name: String? = null
if (name != null) {
println("Name: $name")
} else {
println("Name is not set")
}
// Or using the Elvis operator
val displayName = name ?: "Guest"
println(displayName)