Created
February 23, 2022 14:28
-
-
Save over40dev/a61f100b759a8d69599a75bb23e1d114 to your computer and use it in GitHub Desktop.
Clean Code - IF Statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See [basarat] (https://github.com/basarat/demo-guard-clauses) | |
class Person { | |
age?: number; | |
experience?: number; | |
} | |
// demo-guard-clauses | |
function update(button: number, person: Person | null) { | |
if (button !== 1 || !person) { return; } | |
if (person.age == null) { throw new Error('Person age not set'); } | |
if (person.experience == null) { throw new Error('Person experience not set'); } | |
person.experience += 10; | |
person.age++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment