samedi 1 septembre 2018

Is it good practice if container's size is validated and accessing an element under same conditional statement?

Which one of the following code is more preferable between two of them and why?

1.

std::stack<int>stk;

//Do something

if( stk.empty() == true || stk.top() < 10 )
{
    //Do something.
}

or

2

std::stack<int>stk;

//Do something

if( stk.empty() == true )
{
    //Do something.
}
else if( stk.top() < 10 )
{
    //Do something.
}

Aucun commentaire:

Enregistrer un commentaire