Happy Path

Examples

Simple logger

Go
// Before (bad)
func (l *Logger) Debugf(format string, args ...any) {
	if l.threshold <= LevelDebug {
		fmt.Printf(format+"\n", args...)
	}
}

You should always choose to align the happy path unindented.
Deal with errors and early exits inside your if blocks, and keep real business logic as far left as possible.
This helps a lot when reading the code and makes extending it much easier.

Go
// After (good)
func (l *Logger) Debugf(format string, args ...any) {
	if l.threshold > LevelDebug {
		return // early return guard clause
	}

	fmt.Printf(format+"\n", args...)
}