The requested page is not available for the requested platform. You are viewing the content for Default platform.

Nested code can be flattened

CodeRush Classic shows the Nested code can be flattened code issue if a conditional expression can be converted into a guard clause to flatten the nested code.

Fix

Convert the conditional expression into a guard clause.

Purpose

Highlights the conditional expressions, which can be flattened to improve code readability.

Example

public void AddBook(string bookName)
{if (!Books.Contains(bookName))
    {
        Books.Add(bookName);
        SortBooks();
        OnBookAdded(bookName);
    }
}

Fix:

public void AddBook(string bookName)
{
    if (Books.Contains(bookName))
        return;
    Books.Add(bookName);
    SortBooks();
    OnBookAdded(bookName);
}