重构 代码坏味道的几个典型

sg552sg552 · 2013年04月04日 · 最后由 kakaxi77749 回复于 2013年05月29日 · 3521 次阅读
  1. 嵌套层数过多。比如:

if xx.blank? if yy.present? if zz.valid? .... else... else... else...

  1. COPY 的太严重。例如:

file1

step1_get_this_file(file1) step2_process_this_file_locally(file1) step3_process_this_file_on_remote_server(file1)

file2

step1_get_this_file(file2) step2_process_this_file_locally(file2) step3_process_this_file_on_remote_server(file2)

file3

step1_get_this_file(file3) step2_process_this_file_locally(file3) step3_process_this_file_on_remote_server(file3)

  1. 不合适的命名

green_color = 'red' # 意图 跟实际的值是相反的 success_response = 'false'
this_year = '1999'
var1 = 30 # 无意义的变量名 wxxioks = 4 # 我也不知道这个缩写是啥

  1. 冗余的代码。比如,一个系统中,从来用不到 XML,结果每个 action 都会有:

respond_to do |format| format.html { } format.xml { render :xml => } end

  1. god class /method . 这个不提了。据说有兄弟见过 9K 行的一个 class。我只能膜拜了。。。

艺术来源于生活。吐槽工作贴,鉴定完毕。

推荐 编写可读代码的艺术

+1 条:不按约定格式化的代码和文档

#1 楼 @sundevilyang 估计 LZ 估计读这样的代码不利于养生!

重复代码是最需要避免的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号