clean-code-error-handling

Installation
SKILL.md

Error Handling

Error handling matters, but if it obscures the logic it's wrong. Clean code is readable and robust, and the two goals don't conflict once error handling is treated as a separate concern.

1. Use exceptions (or your language's idiom) rather than return codes

Return codes and error flags clutter the caller, which must check immediately and tends to forget. With exceptions, the main algorithm reads linearly and the error handling sits apart from it.

Language note: where explicit error values are the idiom (Go error, Rust Result, functional Either), use them. Apply the spirit by propagating with the idiomatic shorthand, keeping the happy path linear and handling errors at the layer that can actually do something about them.

2. Write the try/catch/finally first

A try block works like a transaction: execution may abort anywhere inside it, and the catch must leave the program in a consistent state. When writing code that can fail:

  1. Start with a test that forces the failure.
  2. Write the try/catch and the error path first, with the scope defined.
  3. Then build the logic inside, test-driven.
Installs
1
First Seen
2 days ago