markjprice / cs11dotnet7

Repository for the Packt Publishing book titled "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" by Mark J. Price
566 stars 206 forks source link

if statement used without braces in the PagingProducts() method on page 518 #67

Open MAS-OUD opened 1 year ago

MAS-OUD commented 1 year ago

On page 518, the if statement in the method PagingProducts() is used without braces:

if (key == ConsoleKey.LeftArrow)
    if (currentPage == 0)
        currentPage = totalPages;
    else
        currentPage--;
else if (key == ConsoleKey.RightArrow)
    if (currentPage == totalPages)
        currentPage = 0;
    else
        currentPage++;
else
    break; // out of the while loop.

I think it should be written with braces:

if (key == ConsoleKey.LeftArrow)
{
    if (currentPage == 0)
    {
        currentPage = totalPages;
    }
    else
    {
        currentPage--;
    }
}
else if (key == ConsoleKey.RightArrow)
{
    if (currentPage == totalPages)
    {
        currentPage = 0;
    }
    else
    {
        currentPage++;
    }
}
else
{
    break; // out of the while loop.
}

Or in the more concise form:

if (key == ConsoleKey.LeftArrow)
{
    currentPage = (currentPage == 0) ? totalPages : currentPage - 1;
}
else if (key == ConsoleKey.RightArrow)
{
    currentPage = (currentPage == totalPages) ? 0 : currentPage + 1;
}
else
{
    break; // out of the while loop.
}