簡體   English   中英

在返回值和“回調”之間做出決定時的樣式與一致性

[英]Style vs. Consistency when deciding between return value and 'callback'

我當前正在重新編寫的代碼的工作原理如下(C ++ 11之前的版本):

FOO.CPP

if (CONDITION)
{
    SyncThing thing;
}
else
{
    AysncThing thing;
}

complete(bool isSuccess)
{
    // parses status and clears up
}

其中SyncThing和AsyncThing如下:

同步CPP

SyncThing::SyncThing()
{
    // some complex synchronous stuff that calls Foo::complete() when done.
}

異步CPP

AsyncThing::AsyncThing()
{
    // some complex asynchronous stuff that calls Foo::complete() when done.
}

為了不讓構造函數做復雜的事情,並避免在完成后避免構造函數調用Foo :: complete()的非顯而易見的代碼路徑,我想從SyncThing的構造函數中刪除復雜的東西,並將其放在一個單獨的函數中,該函數稱為根據Foo.cpp的邏輯,如下所示:

FOO.CPP

if (CONDITION)
{
    SyncThing thing;
    bool didTheThing = thing.doSomeComplexSyncStuff();       
    complete(didTheThing);
}
else
{
    AysncThing thing;
}

complete(bool isSuccess)
{
    // parses status and clears up
}

我認為這使代碼路徑更易於遵循。

我的問題如下:

  1. 忽略AsyncThing, 我的SyncThing更好更簡單布爾回報版本比有SyncThing或SyncThing :: doSomeComplexSyncStuff(的構造函數)調用foo ::完成()的時候做了什么?

  2. 總體而言,考慮到AsyncThing,有必要返回“回調”樣式,是否最好保持一致性並對SyncThing進行相同處理,即使對1.的回答為肯定?

非常感謝!

在我看來,最好的事情是對SyncThingAsyncThing做類似SyncThing.setCompletedFunction(&complete)AsyncThing 這兩個都清楚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM