| name | fail-fast |
| description | Guard clauses instead of nested if/else in Kotlin — require/requireNotNull/check matched to the original exception type, flattening nested pyramids into sequential early returns, "?: return" chains for nullables, resource cleanup on every early-return path, and when not to invert a branch. Use for any code with preconditions, nullable values, or nested conditionals. |
Fail Fast: Guard Clauses Over Nested if/else
Write new conditionals this way, and invert Java's nested-if pyramids into it when you
touch them: guard clauses that return (or throw) early, leaving the happy path at the lowest
indentation. Behaviour is identical — the branches are the same, only the shape changes.
Examples come from the wider nextcloud/android codebase; the class names are not from this
repository.
Precondition Checks → require / requireNotNull
requireNotNull returns the smart-cast non-null value AND throws
IllegalArgumentException with the message — exactly matching the Java if (x == null) throw new IllegalArgumentException(...).
if (file == null) throw IllegalArgumentException("File may not be null");
if (user == null) throw IllegalArgumentException("Account may not be null");
fileActivity = (FileActivity) getActivity();
if (fileActivity == null) throw IllegalArgumentException("FileActivity may not be null");
fileActivity = activity ? FileActivity
requireNotNull(file) { }
requireNotNull(user) { }
requireNotNull(fileActivity) { }