Skip to content

Instantly share code, notes, and snippets.

@Amir-A-M
Created August 21, 2022 14:58
Show Gist options
  • Save Amir-A-M/91109840df401e8556605fb12941163e to your computer and use it in GitHub Desktop.
Save Amir-A-M/91109840df401e8556605fb12941163e to your computer and use it in GitHub Desktop.
Refactor of confusing code - return early pattern
function returnStuff(argument1, argument2) {
if (!argument1.isValid()) {
throw new Exception();
}
if (!argument2.isValid()) {
throw new Exception();
}
const otherVal1 = doSomeStuff(argument1, argument2);
if (!otherVal1.isValid()) {
throw new Exception();
}
const otherVal2 = doAnotherStuff(otherVal1);
if (!otherVal2.isValid()) {
throw new Exception();
}
return "Stuff";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment