开发者

Remove Duplicate Calling of function prior to exiting from container function

开发者 https://www.devze.com 2023-02-21 16:46 出处:网络
Consider the following code snippet: void MyFun() { SetMe(); // more code if (.. ) { UnSetMe(); return; } // more codes

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

精彩评论

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

关注公众号