在看,作者提到要避免do/while循環(huán):What’sweirdaboutado/whileloopisthatablockofcodemayormaynotbereexecutedbasedonaconditionunderneathit.Typically,logicalconditionsareabovethecodetheyguard—thisisthewayitworkswithif,while,andforstatements.Becauseyoutypicallyreadcodefromtoptobottom,thismakesdo/whileabitunnatural.Manyreadersendupreadingthecodetwice.但do/while有時候也很適用一些場景,比如前兩天我在一個問題里回答:do{result=msgrcv(id,(void*)&msg,sizeof(msg.text),msgtyp,MSG_NOERROR);}while(result==-1&&errno==EINTR);我們至少需要做一次msgrcv,然后判斷出錯情況是否需要循環(huán).這里do/while就非常適合.問問大家的看法.
代碼可讀性: 避免do/while 循環(huán)
搖曳的薔薇
2019-04-13 08:46:17