Deep nesting is hard to read. Replace nested if blocks with guard clauses that return early.
# dirty — 4 levels deep
def solve(user):
if user:
if user['is_active']:
if user['age'] >= 18:
if user['has_premium']:
return 'full-access'
else:
return 'basic-access'
else:
return 'age-restricted'
else:
return 'inactive'
else:
return 'no-user'Refactor using guard clauses so the happy path is at the end with minimal nesting.
Sample tests