domains / / denoland/deno
prefer safe optional returns
When designing APIs and handling potentially missing or unavailable values, prefer returning optional types (Option
When designing APIs and handling potentially missing or unavailable values, prefer returning optional types (Option
Key practices:
- Return
Noneinstead of usingunreachable!()for cases that might become reachable in the future - Use
Option<T>for platform-specific values that may not be available everywhere - Handle errors explicitly rather than using
unwrap()which can panic - Avoid “rogue None” parameters by designing specialized APIs when possible
Example from the discussions:
// Instead of this (can panic if plugin changes):
_ => unreachable!(),
// Prefer this (safe fallback):
_ => None,
// For platform-specific values:
// Instead of: Result<(ResourceId, IpAddr, IpAddr, Fd), NetError>
// Use: Result<(ResourceId, IpAddr, IpAddr, Option<Fd>), NetError>
// Handle errors instead of panicking:
// Instead of: parsed_source.specifier().to_file_path().unwrap()
// Use proper error handling with Result types
This approach makes code more robust, prevents runtime panics, and clearly communicates when values might be absent through the type system.