Loading...
Loading...
Odin coding standards. Use when writing Odin code. Covers idiomatic error handling with or_else and or_return, memory/allocator patterns, attributes, naming and API conventions, and testing with core:testing.
npx skill4agent add lgtm-hq/ai-skills stand-odinor_else// Don't — all branches return the same fallback
parse_env_int :: proc(key: string, default: int = 0) -> int {
v, ok := os.lookup_env(key, context.temp_allocator)
if !ok || v == "" { return default }
x, ok2 := strconv.parse_int(strings.trim_space(v), 10)
if !ok2 { return default }
return x
}
// Do
parse_env_int :: proc(key: string, default: int = 0) -> int {
v := os.lookup_env(key, context.temp_allocator) or_else ""
return strconv.parse_int(strings.trim_space(v), 10) or_else default
}parse_env_f64_nonneg :: proc(key: string, default: f64 = 0) -> f64 {
v := os.lookup_env(key, context.temp_allocator) or_else ""
x := strconv.parse_f64(strings.trim_space(v)) or_else default
return max(0, x)
}or_return// Don't
data, err := os.read_entire_file_or_err(path)
if err != nil { return nil, err }
// Do
data := os.read_entire_file_or_err(path) or_return(value, ok)@(private = "file")
trimmed :: proc(s: string) -> string { return strings.trim_space(s) }
@(require_results)
checksum :: proc(data: []byte) -> u32 { ... }(value, ok)or_else0defaultv == ""if err != nil { return err }or_returncontext.temp_allocatorfree_all(context.temp_allocator)context.allocatormakenewdeletefreedeferallocator := context.allocator// Don't — caller cannot control allocation
clone_name :: proc(s: string) -> string { return strings.clone(s) }
// Do — caller controls allocation; allocator errors propagate
clone_name :: proc(s: string, allocator := context.allocator) -> (string, mem.Allocator_Error) {
return strings.clone(s, allocator)
}or_elseor_returnok(value, ok)panicassert#assertsnake_caseAda_CaseEntity_KindParse_ErrorSCREAMING_SNAKE_CASEparse_env_f64_nonnegmax(0, x)proccore:testing@(test)odin testimport "core:testing"
@(test)
parse_env_int_defaults :: proc(t: ^testing.T) {
cases := [][2]int{{-3, -3}, {0, 0}, {42, 42}}
for c in cases {
testing.expect_value(t, parse_env_int("MISSING_KEY", c[0]), c[1])
}
}stand-generalodin checkodin build -vet -strict-stylelint