Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active May 11, 2021 17:13
Show Gist options
  • Save NyaGarcia/eff7c4d3e918111b346a268e9185c2ae to your computer and use it in GitHub Desktop.
Save NyaGarcia/eff7c4d3e918111b346a268e9185c2ae to your computer and use it in GitHub Desktop.
Refactoring a function to use guard clauses
function publishTweet(tweet) {
if (!isLoggedIn()) {
throw new Error('You need to log in before tweeting');
}
if (!tweet) {
throw new Error("Your tweet is empty, can't publish it");
}
if (!isTweetDoubleChecked()) {
throw new Error('Dont publish without double checking your tweet');
}
tweetIt(tweet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment