logoalt Hacker News

dieselgate • today at 9:29 PM • 2 replies • view on HN

Didn’t see it mentioned in the article but isn’t leading with if-statement called a “guard clause”. I like that pattern but it’s just general best practice I thought.


Replies

mahboi • today at 9:53 PM

Guard clauses are things that return early for trivial or problematic cases. Like https://en.wikipedia.org/wiki/Guard_(computer_science)#Flatt...

They're one of those good practices that look like bad practice to everyone who just got a CS degree. Seems ex-students are unsettled by asymmetry or want to minimize the number of return statements.

➕ show 1 reply
woadwarrior01 • today at 9:34 PM

Swift explicitly has a guard statement for this. Rust's let .. else { ... } is also very similar.

https://docs.swift.org/latest/documentation/the-swift-progra...