Adopt a consistent null/absence policy:
1) Model absence explicitly
String?) and make the corresponding Codable field tolerant of omission.2) Prefer early-exit unwraps
guard let / if let instead of force unwraps.guard ... else { continue }.3) Use safe collection/dictionary defaults
dict[key]!.dict[key, default: []].append(...) or dict[key]?.append(...).4) Keep Codable backward-compatible
decodeIfPresent) so older files don’t fail to decode.5) Avoid unnecessary optional boilerplate and optional-of-collection
= nil for optional stored properties.[T] over [T]? unless you truly need to distinguish “missing” from “empty”.Example patterns:
// 1) Codable backward compatibility
struct KubeConfig: Codable {
enum CodingKeys: String, CodingKey {
case currentContext = "current-context"
}
var currentContext: String? = nil
}
// 2) Dictionary mutation
allocationsBySession[session, default: []].append((hostname: hostname, index: index))
// 3) Safe unwrap in control flow
guard let lastName = file.lastComponent?.string else { continue }
// 4) Avoid optional-of-collection
struct VolumeListResponse: Codable {
var warnings: [String] = [] // not [String]?
}
// 5) Avoid force unwrap parsing external data
guard let manifest = index.manifests.first else {
throw ContainerizationError(.internalError, message: "Malformed image index")
}
Apply this standard anywhere you: