Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
mobile ruleP1universalStack: mobile
mobileenumdecodingrobustness
Handle unknown/unmapped backend enum values gracefully
When decoding a string into an enum, account for values the app doesn't know (new server states) with a default/unknown case instead of crashing or misbehaving.
PR: mobile-mining-2026-06Created: Jul 5, 2026
Bad example
Old codeswift
| 1 | enum HistoryType: String { case sold, bought } // decode fails on "PENDING" |
Explanation (EN)
Objašnjenje (HR)
Good example
New codeswift
| 1 | enum HistoryType: String, Decodable { case sold, bought, unknown |
| 2 | init(from d: Decoder) throws { self = HistoryType(rawValue: try d.singleValueContainer().decode(String.self)) ?? .unknown } } |
Explanation (EN)
Objašnjenje (HR)