Menu
Coddy logo textTech

Early Returns

Part of the Logic & Flow section of Coddy's C# journey — lesson 18 of 66.

Early returns are a technique where we exit a method as soon as we have the answer or encounter an invalid condition, rather than nesting conditionals.

Let's compare two approaches to validate a username:

Without early returns (nested conditionals):

public bool ValidateUsername(string username)
{
    bool isValid = false;
    
    if (username != null)
    {
        if (username.Length >= 3)
        {
            if (username.Length <= 20)
            {
                isValid = true;
            }
        }
    }
    
    return isValid;
}

With early returns:

public bool ValidateUsername(string username)
{
    // Return early if username is null
    if (username == null)
        return false;
        
    // Return early if username is too short
    if (username.Length < 3)
        return false;
        
    // Return early if username is too long
    if (username.Length > 20)
        return false;
        
    // If we get here, username is valid
    return true;
}

Early returns make code more readable and reduce indentation levels.

challenge icon

Challenge

Easy

Write a method called ProcessPayment that determines if a payment can be processed based on:

  1. The payment amount must be positive
  2. The account balance must be sufficient
  3. The account must not be locked

Use early returns to check each condition and return appropriate error messages.

The method should:

  • Take three parameters: decimal paymentAmount, decimal accountBalance, and bool isAccountLocked
  • Return a string with either "Payment processed successfully" or an error message
  • Use early returns for validation

Print error messages in exactly these formats:

  • "Error: Payment amount must be positive"
  • "Error: Insufficient funds"
  • "Error: Account is locked"

Cheat sheet

Early returns exit a method immediately when a condition is met, avoiding nested conditionals and reducing indentation.

Without early returns (nested conditionals):

public bool ValidateUsername(string username)
{
    bool isValid = false;
    
    if (username != null)
    {
        if (username.Length >= 3)
        {
            if (username.Length <= 20)
            {
                isValid = true;
            }
        }
    }
    
    return isValid;
}

With early returns:

public bool ValidateUsername(string username)
{
    // Return early if username is null
    if (username == null)
        return false;
        
    // Return early if username is too short
    if (username.Length < 3)
        return false;
        
    // Return early if username is too long
    if (username.Length > 20)
        return false;
        
    // If we get here, username is valid
    return true;
}

Early returns make code more readable and reduce indentation levels.

Try it yourself

using System;

public class Program
{
    public static string ProcessPayment(decimal paymentAmount, decimal accountBalance, bool isAccountLocked)
    {
        // Write your code here
    }
    
    public static void Main(string[] args)
    {
        decimal paymentAmount = decimal.Parse(Console.ReadLine());
        decimal accountBalance = decimal.Parse(Console.ReadLine());
        bool isAccountLocked = bool.Parse(Console.ReadLine());
        
        string result = ProcessPayment(paymentAmount, accountBalance, isAccountLocked);
        Console.WriteLine(result);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow