设为首页 加入收藏

TOP

引入中间变量使程序更易读
2014-11-24 00:33:05 来源: 作者: 【 】 浏览:21
Tags:引入 中间 变量 程序 易读

今天查看自己为《专业嵌入式软件开发》一书所写的代码时发现,个别函数由于没有引入中间变量,使代码行既长又不易读。重构前后的代码如下所示。

重构前:
if (TIMER_STARTED == _handle->state_) {
timer_handle_t next;

if (g_timer_next == _handle) {
g_timer_next = (timer_handle_t) dll_next (&g_bucket_firing->dll_,
&_handle->node_);
}
next = (timer_handle_t)dll_next
(&g_buckets [_handle->bucket_index_].dll_, &_handle->node_);
if (0 != next) {
next->round_ += _handle->round_;
}
dll_remove (&g_buckets [_handle->bucket_index_].dll_, &_handle->node_);
if (g_buckets [_handle->bucket_index_].reentrance_ > 0) {
g_bucket_firing->level_ ++;
}
}

重构后:
if (TIMER_STARTED == _handle->state_) {
timer_handle_t next;
bucket_t *p_bucket = &g_buckets [_handle->bucket_index_];

if (g_timer_next == _handle) {
g_timer_next = (timer_handle_t) dll_next (&g_bucket_firing->dll_,
&_handle->node_);
}
next = (timer_handle_t)dll_next (&p_bucket->dll_, &_handle->node_);
if (0 != next) {
next->round_ += _handle->round_;
}
dll_remove (&p_bucket->dll_, &_handle->node_);
if (p_bucket->reentrance_ > 0) {
g_bucket_firing->level_ ++;
}
}


作者 李云

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇无意间溢出修改变量值 下一篇C语言函数动态调用(参考UnderC实..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: