Chinaunix首页 | 论坛 | 博客
  • 博客访问: 881515
  • 博文数量: 61
  • 博客积分: 2315
  • 博客等级: 大尉
  • 技术积分: 2560
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-22 18:00
文章分类

全部博文(61)

文章存档

2015年(3)

2014年(3)

2013年(9)

2012年(23)

2011年(23)

分类: C/C++

2012-05-10 14:28:43

今天查看自己为《专业嵌入式软件开发》一书所写的代码时发现,个别函数由于没有引入中间变量,使代码行既长又不易读。重构前后的代码如下所示。
 
重构前:
  1. if (TIMER_STARTED == _handle->state_) { 
  2.     timer_handle_t next; 
  3.      
  4.     if (g_timer_next == _handle) { 
  5.         g_timer_next = (timer_handle_t) dll_next (&g_bucket_firing->dll_,  
  6.             &_handle->node_); 
  7.     } 
  8.     next = (timer_handle_t)dll_next  
  9.         (&g_buckets [_handle->bucket_index_].dll_, &_handle->node_); 
  10.     if (0 != next) { 
  11.         next->round_ += _handle->round_; 
  12.     } 
  13.     dll_remove (&g_buckets [_handle->bucket_index_].dll_, &_handle->node_); 
  14.     if (g_buckets [_handle->bucket_index_].reentrance_ > 0) { 
  15.         g_bucket_firing->level_ ++; 
  16.     } 
 
重构后:
  1. if (TIMER_STARTED == _handle->state_) { 
  2.     timer_handle_t next; 
  3.     bucket_t *p_bucket = &g_buckets [_handle->bucket_index_]; 
  4.      
  5.     if (g_timer_next == _handle) { 
  6.         g_timer_next = (timer_handle_t) dll_next (&g_bucket_firing->dll_,  
  7.             &_handle->node_); 
  8.     } 
  9.     next = (timer_handle_t)dll_next (&p_bucket->dll_, &_handle->node_); 
  10.     if (0 != next) { 
  11.         next->round_ += _handle->round_; 
  12.     } 
  13.     dll_remove (&p_bucket->dll_, &_handle->node_); 
  14.     if (p_bucket->reentrance_ > 0) { 
  15.         g_bucket_firing->level_ ++; 
  16.     } 
本文出自李云的博客,请务必保留此出处:http://blog.chinaunix.net/uid-26470037-id-3203040.html。
阅读(4221) | 评论(3) | 转发(1) |
给主人留下些什么吧!~~

suiyuemanbu2015-03-06 17:40:01

哈,很幸运,这个习惯我早已有之

十七岁的回忆2012-05-12 16:22:53

不错的习惯,建议大家可以学习一下

十七岁的回忆2012-05-10 21:16:03

晚辈学习了,感觉路漫漫修远啊~~