Remove Duplicate Calling of function prior to exiting from container function
Consider the following code snippet:
void MyFun()
{
SetMe();
// more code
if (.. )
{
UnSetMe();
return;
}
// more codes
if ( .. )
{
UnSetMe();
return ;
}
// more code
UnSetMe();
}
As you can see, the function first calls SetMe() and then prior to terminating, calls UnSetMe(). Now its little bit cumbersome to add UnSetMe() at every places where the function has to exit and one may omit it by mistake in some places.开发者_如何学JAVA
What would be an improved design to handle such situation.
You could call MyFun() from MyWrapperFun(), and then call UnsetMe() after:
void MyWrapperFun()
{
MyFun();
UnsetMe();
}
精彩评论