Categories
Android

Code Review

每周六下午都对自己的代码进行一次 code review.通过 svn log记录,总结主要修改的:

1. 先阅读一遍Android编码风格
2. 添加文件头版权信息
3. 添加注释
4. tab to 4 spaces , 通过 trac 阅读代码,很容易找出。
5. 重命名函数名及参数,让它看起来更合理。
6. 去掉 magic number
7. 删除重复的代码,没用的代码
8. 减少大函数,拆分成小函数
9. 源代码后面要空一行
10. 更新设计文档

对于第7点,比如boolean 函数力求简凑,

-            if (deviceRegistrationId == null) {
-                return false;
-            } else {
-                return true;
-            }
+
+            return (deviceRegistrationId != null);

下面是对应的 ddx 格式

133     .local v0, deviceRegistrationId:Ljava/lang/String;
134     if-nez v0, :cond_0
135     
136     .line 383
137     const/4 v2, 0x0
138     
139     .line 385
140     :goto_0
141     return v2
142 
143     :cond_0
144     const/4 v2, 0x1
145 
146     goto :goto_0
147 .end method


133     .local v0, deviceRegistrationId:Ljava/lang/String;
134     if-eqz v0, :cond_0
135 
136     const/4 v2, 0x1
137 
138     :goto_0
139     return v2
140 
141     :cond_0
142     const/4 v2, 0x0
143 
144     goto :goto_0

编译器编译出来的 ddx是一样的。

If you enjoyed this post, make sure you subscribe to my RSS feed!

2 replies on “Code Review”

[…] 完成了这些,到这里先打住,定时的回头自己给自己做 code review,删除没用的代码,要下狠心清理掉垃圾代码,对代码有洁癖感。顺便提一提实际开发过程中需要注意的,这些是初学开发者累犯的错误,有的甚至是有经验的工程师,平时对自己要求不严,就容易犯下这样的错误。 […]

Leave a Reply

Your email address will not be published. Required fields are marked *