爛 code 收集器//添加邏輯後,要整理

  1. 爛 code 收集器//添加邏輯後,要整理

爛 code 收集器//添加邏輯後,要整理

原文連結: https://darkblack02.blogspot.com/2018/05/code_55.html
移植時的最後更新日期: 2020-02-28T10:41:21.432+08:00

複雜的邏輯往往來自於不同時期的工程進行的添加。
一流的工程師會寫出讓人好讀的程式碼,一樣一流的工程師接手會讀懂後,添加在適合的位置,而不是一直往巢狀式的方向加上去。


for( set::iterator it = ObjSet.begin(); it != ObjSet.end(); ++it )
{
Obj *target_obj = 0;

if( (*it)->IsSpec == false ) //如果有使用 spec
{
target_obj = (*it);
}
else // 並且 spec 已經有使用了
{
if( SpecInCurrProduct.count((*it)->getSpec) != FALSE )
{
target_obj = (*it);
}
}

if( target_obj == 0 )
{
continue;
}

//other code…
}

改寫成

for( set::iterator it = Obj1Set.begin(); it != Obj1Set.end(); ++it )
{
Obj1 *target_obj1 = 0;

if( !(*it)->IsSpec || SpecInCurrProduct.find((*it)->getSpecId) != SpecInCurrProduct.end() )
target_obj1 = (*it);
// else
// continue;
//other code…
}