Early exits
aka the Bouncer Patter or guard clauses
function transformData(rawData) {
// check if no data
if (!rawData) {
return [];
}
// check for specific case
if (rawData.length == 1) {
return [];
}
// actual function code goes here
return rawData.map((item) => item);
}
encourages thinking around invalid/edge cases and how those cases should be handled
avoids accidental and unnecessary processing of code against an unexpected use case
mentally allows me to process each use case much more clearly
once adopted, you can quickly glance at functions and understand the flow and execution which typically follows a top down approach going from - invalid cases -> small cases -> expected case
Last updated
Was this helpful?