开发者

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜