论坛首页 Java企业应用论坛

实践中的重构12_不要乱用异常

浏览 5506 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-12-30  
原来代码是一个,经过lz重构后变成了一坨。。。
原来代码固然不是个好方法,经过lz重构后的代码虽然完成了所需的期望,但是以牺牲可读性为代价的。满足你的希望,没有错,可你不能强加你的思想于别人,那等同于弓虽女干.可以感觉到你的水平其实并不比原代码作者高哪里去。
0 请登录后投票
   发表时间:2010-12-30  
ouchxp 写道
LZ对重构的理解有偏差..

重构不是改BUG.
重构不是讲解代码规范
重构不是优化代码效率.

重构的目的是在不改变"软件之可察行为"前提下,提高其可理解性,降低其修改成本.


标题的重构的确有一定的误导性。这个我近期会改掉的。
0 请登录后投票
   发表时间:2010-12-30   最后修改:2010-12-30
异常还是不要乱抛的好。我觉得用regex判断会更优雅。
0 请登录后投票
   发表时间:2010-12-30  
超级潜水员 写道
重构出来的代码跟坨屎一样.

+1
0 请登录后投票
   发表时间:2010-12-30  
logicgate 写道
异常还是不要乱抛的好。

有些人说楼主重构的代码象一砣。。。其实可能是楼主为了说明问题,直接把代码放在这里了。

实际项目中如果经常需要判断数字,完全可以把这段代码提取出来放到utility class里。


你的意思是,乾坤大挪移后,东西变好了是吧。

第一种写法,代码很清晰,你称之为 乱抛异常吗?
0 请登录后投票
   发表时间:2010-12-30  
字符串转换为数值用异常没有这么大的罪吧(虽然程序的本意是进行数值格式检查),当然了,用异常比不用异常性能会差一点,但也比楼主所讲的方法效率高吧。
0 请登录后投票
   发表时间:2010-12-30   最后修改:2010-12-30
gdpglc 写道
logicgate 写道
异常还是不要乱抛的好。

第一种写法,代码很清晰,你称之为 乱抛异常吗?

《Effective Java》 chapter 8:
引用
• Because exceptions are designed for use under exceptional circumstances, few, if any, JVM implementations attempt to optimize their performance. It is generally expensive to create, throw, and catch an exception.
• Placing code inside a try-catch block precludes certain optimizations that modern JVM implementations might otherwise perform.
• The standard idiom for looping through an array does not necessarily result in redundant checks; some modern JVM implementations optimize them away.

The moral of this story is simple: Exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow. A well-designed API must not force its client to use exceptions for ordinary control flow.

最重要的是,这里明明不用异常就可以把事情做的更好。
0 请登录后投票
   发表时间:2010-12-30  
logicgate 写道
gdpglc 写道
logicgate 写道
异常还是不要乱抛的好。

第一种写法,代码很清晰,你称之为 乱抛异常吗?


《Effective Java》 chapter 8:

引用
• Because exceptions are designed for use under exceptional circumstances, few, if any, JVM implementations attempt to optimize their performance. It is generally expensive
to create, throw, and catch an exception.
• Placing code inside a try-catch block precludes certain optimizations that modern
JVM implementations might otherwise perform.
• The standard idiom for looping through an array does not necessarily result in
redundant checks; some modern JVM implementations optimize them away.

The moral of this story is simple: Exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow. A well-designed API must not force its client to use exceptions for ordinary control flow.


最重要的是,这里明明不用异常就可以把事情做的更好。


书是用来活学活用的,不是用来做教条的。

不过是一个数字验证,用不着上纲上线。
0 请登录后投票
   发表时间:2010-12-30   最后修改:2010-12-30
gdpglc 写道
书是用来活学活用的,不是用来做教条的。

不过是一个数字验证,用不着上纲上线。

勿以恶小而为之,勿以善小而不为。
0 请登录后投票
   发表时间:2010-12-30   最后修改:2010-12-30
logicgate 写道
gdpglc 写道
书是用来活学活用的,不是用来做教条的。

不过是一个数字验证,用不着上纲上线。

不以恶小而为之,不以善小而不为。

---------------------------
看到你的回复,我想起一句话:中国很多程序员是认认真真的制造垃圾代码。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics