标签:style class blog color get strong
代码块:完成一定功能的一系列的代码 ,或者是几行代码 或者是 连续调用几个函数。
将代码分割成代码块,或者通过分割成单独的函数 又或者 仅是录入几行空白部分实现。
在完成一定功能的代码块中最好不要加入其他功能的代码 且 要注意代码块的顺序性 尤其是在代码块间存在着数据共享。
上文源于实际开发中、且代码排查花费了12人时。
伪代码举例:
①、存在TimerManager类,其属性有List<ActionTimer> list、方法有removeTimer(id:String)、run():每秒都遍历计时器list,
②、存在ActionTimer接口,其属性有leftTime、timerId、方法有run():剩余时间减一并产生相应的变化,runOver():剩余时间为0后发生的变化。
出现的问题是:在部分计时器剩余为0时,list的remove(index)报越界错误:index 大于了 list.length。
private function run():void
{
for(index;index<length;index++)
{
if(list[index].leftTime>0)
{
list[index].run();
}
else
{
list.remove(index); //位置①
lenght--; //位置②
index--; //位置③
list[index].runOver(); //代码①
}
}
}
public function removeTimer(timerId:String):void
{
for遍历List<ActionTimer>;
如果找到actionTimer.getTimerId() == timerId ;
则执行list.remove(index) 移除该actionTimer; //该操作会影响list的length
}
具体原因是:有人在继承 ActionTimer接口的类中的runOver()中再次调用TimerManager.removeTimer(this.timerId)、且代码①的位置放到了位置①的前边。
深层原因:错误排查人员菜鸟一枚、 继承的ActionTimer接口的类可读性差、未重视继承的ActionTimer接口的类的runOver()排查不全面、开发人员间沟通不充分致使TimerManager类的功能未充分了解同时多一次调用了remove()。
标签:style class blog color get strong
原文地址:http://www.cnblogs.com/ribavnu/p/3799313.html