Return Early Pattern

Syed Taha Alam
3 min readFeb 16, 2023

One of the most important factor regarding the code written in clean code architecture is its readability. Code readibility is something makes the code stand out and the code becomes easier to be maintained by the new developers in the team.

Return early Pattern can be defined as:

Return early is the way of writing functions or methods so that the expected positive result is returned at the end of the function and the rest of the code terminates the execution (by returning or throwing an exception) when conditions are not met.

let me explain it with an example. Following is the code that does not follow the pattern

fun performSignup(firstName, lastName, userName){

if(firstName.validate()){

if(lastName.validate()){
val isUserNamExist = checkUserName(userName)

if(isUserNamExist){
signupUseCase.performSignup(firstName,lastName,userName)
}else{
throw UserNameNotAvailableException()
}

}else{
throw LastNameNotValidException()

}

}else{
throw FirstNameNotValidException()

}

}

Things that can be seen:

  • Non-linear flow.
  • the happy path is hidden inside a jungle of if/else
  • Too many indentation which makes it difficult to check the corresponding else.
  • In this example only exceptions are thrown but if the else does something than it will be difficult to understand the execution and will lead to the unnecessary errors.

and now the code that follows it:

fun performSignup(firstName, lastName, userName){

if(!firstName.validate()){
throw FirstNameNotValidException()
}

if(!lastName.validate()){
throw LastNameNotValidException()
}


val isUserNamExist = checkUserName(userName)


if(!isUserNamExist){
throw UserNameNotAvailableException()
}


signupUseCase.performSignup(firstName,lastName,userName)

}

Not following the code will add some anti pattern to the code that are as follows:

On the other side the Few things can be observed from the code that follows the pattern are:

  • It makes it easier for the test driven development as the code fails earlier.
  • Easier to understand what is being done because it has less indentation.

By Only following this pattern code will achieve some of the other design pattern also.

Fail Fast It was proposed in 2004 that states that Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly flawed process.

Bouncer Pattern is a very simple technique that can make your code more readable. It shines when it comes to pieces of code with many if/else statements and you want to get rid of some unwanted code indentation.

In summary, using the “return early” pattern can effectively prevent functions from becoming convoluted, but it may not always be applicable, particularly in cases involving intricate business logic that require nested “if” statements or the extraction of code to other functions. Therefore, it is important to collaborate with your team, share knowledge, and collectively determine which patterns are best suited for each situation. This ensures that everyone is on the same page when it comes to programming and helps create a positive experience for developers, who spend more time reading code than writing it.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Syed Taha Alam
Syed Taha Alam

Written by Syed Taha Alam

Software Engineer passionate about the web and all the latest tech 🌐

No responses yet

Write a response