19-04-2018, 04:24 PM
Hello,
We are interested about id 0000023 but in C++.
We would like to be MISRA compatible.
But forbid multiple return in some case make the code less understandable.
Further more with the use of RAII, it is pretty safe to have multiple return inside a function.
This rule inherited from C where multiple return is highly unsafe is about to evolve in new version of MISRA C++ ?
If not, this is a "Required" rules, could it be added to a "deviation permits" ?
Example:
If we transform this code using "early return pattern" method which is not MISRA on rule 6-6-5.
into MISRA
If you miss to add a test somewhere like for testing "again" currentObject before to use it, you'll get a possible crash....
It increase complexity of code and make it unsecured.
Readability is something very important to get trusty code.
You can also transform it to another form
But the readability is less good than "early return pattern", because you "jump" from "if" to "if" and the last part to execute some code is subject to errors (or after any else), if no test are added before the return statement to test the error state.
ps:
In MISRA 2008 document chapter 6-6-5, there is a synthax mistake on fn3, there is missing {}
We are interested about id 0000023 but in C++.
We would like to be MISRA compatible.
But forbid multiple return in some case make the code less understandable.
Further more with the use of RAII, it is pretty safe to have multiple return inside a function.
This rule inherited from C where multiple return is highly unsafe is about to evolve in new version of MISRA C++ ?
If not, this is a "Required" rules, could it be added to a "deviation permits" ?
Example:
If we transform this code using "early return pattern" method which is not MISRA on rule 6-6-5.
Code:
int CheckIsOk()
{
if (currentObject == NULL)
{
return E_ERR_1;
}
if (!IsClassValid(id))
{
return E_ERR_2;
}
currentObject->test();
return E_OK;
}
into MISRA
Code:
int CheckIsOk()
{
int err = E_OK;
if (err == E_OK && currentObject == NULL)
{
err = E_ERR_1;
}
if (err == E_OK && !IsClassValid(id))
{
err = E_ERR_2;
}
currentObject->test(); // Developer miss to add "err == E_OK && "
return err;
}
If you miss to add a test somewhere like for testing "again" currentObject before to use it, you'll get a possible crash....
It increase complexity of code and make it unsecured.
Readability is something very important to get trusty code.
You can also transform it to another form
Code:
int CheckIsOk()
{
int err = E_OK;
if (currentObject)
{
if (IsClassValid(id))
{
currentObject->test();
}
else
{
err = E_ERR_2;
}
}
else
{
err = E_ERR_1;
}
// "Difficulties" to add code here
if( err == E_OK)
{
// Do something
}
return err;
}
But the readability is less good than "early return pattern", because you "jump" from "if" to "if" and the last part to execute some code is subject to errors (or after any else), if no test are added before the return statement to test the error state.
ps:
In MISRA 2008 document chapter 6-6-5, there is a synthax mistake on fn3, there is missing {}
<t></t>