Checking if a username is valid I'm learning about how to create a good and clean code. I just need to review something like that, and I really not sure how can I fix this code into a clean code.
bool isValidUserName(const std::string&amp; username)
{
    bool valid = false;

    if (username.length() &gt;= MIN_NAME_LENGTH)
    {
        if (username.length() &lt;= MAX_NAME_LENGTH)
        {
            if (isalpha(username[0]))
            {
                bool foundNotValidChar = std::find_if(username.begin(), username.end(), isNotValid) != username.end();
                if (!foundNotValidChar)
                {
                    valid = true;
                }
            }
        }
    }

    return valid;
}
We can avoid the excessive indentation by returning early.
Instead of checking for validity:
if (username.length() &gt;= MIN_NAME_LENGTH)
{
    ... additional nested checks...
}

we reverse the check and return as soon as possible:
if (username.length &lt; MIN_NAME_LENGTH)
    return false; // done!

So overall we'd get something like:
if (username.length() &lt; MIN_NAME_LENGTH || username.length() &gt; MAX_NAME_LENGTH)
    return false;

if (username.empty() || !std::isalpha(static_cast&lt;unsigned char&gt;(username.front()))) // paranoia: avoid crashing with empty username
    return false;

if (std::find_if(username.begin(), username.end(), isNotValid) != username.end())
    return false;

return true;


Note that isalpha is in the std:: namespace in C++. Technically we should also cast to unsigned char when calling the function. (We could wrap that in a function of our own).