当前位置:编程学习 > C/C++ >>

引入中间变量使程序更易读

今天查看自己为《专业嵌入式软件开发》一书所写的代码时发现,个别函数由于没有引入中间变量,使代码行既长又不易读。重构前后的代码如下所示。
 
重构前:
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语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,