【编程好习惯】引入中间变量使程序更易读
今天查看自己为《专业嵌入式软件开发》一书所写的代码时发现,个别函数由于没有引入中间变量,使代码行既长又不易读。重构前后的代码如下所示。
重构前:
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_ ++;
}
}
本文出自 “至简李云” 博客,请务必保留此出处http://yunli.blog.51cto.com/831344/859794
相关新闻>>
- 发表评论
-
- 最新评论 更多>>