Adding Sugar for Preconditions and Guards

I know it's not the answer you're looking for, but just about all of my swift code doesn't have unnecessary empty unwraps. Usually it's a precondition resulting in a non-void return value. When reviewing code, seeing a line like that is code smell for me. If the function takes an optional and returns early without doing anything, then maybe it shouldn't take an optional value in the first place. I've battled with this a few times before, but if it really can't be avoided I always suggest to use that empty line before return to print a log message.

If anything the block in guard could adopt SE-0255: Implicit Returns from Single-Expression Functions, and then the syntax would be:

guard let foo = foo else {}

Which is almost exactly as long as:

precondition let foo = foo

While also letting git diffs look pretty when someone eventually does add in that print statement.

1 Like