你允许多少重复的代码? [英] How much duplicated code do you tolerate?

查看:235
本文介绍了你允许多少重复的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的代码审查中,我发现了一个类中的几行重复的逻辑(少于15行)。当我建议作者重构代码时,他认为代码更容易理解。在再次读取代码后,我不得不同意提取重复的逻辑会伤害可读性一点。



我知道DRY是指南,而不是绝对规则。但一般来说,你是否愿意以DRY的名义伤害可读性?

解决方案

重构:改进现有代码的设计



三个规则


第一次做某事时,你
它。第二次你做了

类似的东西,你在重复的时候,但你做重复的

的东西。


三次罢工,你重构。

>




这些XII调用你正在写一个
实现。

你发现你积累了大量的
位的非常相似的代码吗?



Zawinski:哦,是的,绝对。通常第二次或第三次
你已经剪切和粘贴

那段代码就好了,好吧,时间停止
剪切和粘贴,并把它放在
子例程。



In a recent code review I spotted a few lines of duplicated logic in a class (less than 15 lines). When I suggested that the author refactor the code, he argued that the code is simpler to understand that way. After reading the code again, I have to agree extracting the duplicated logic would hurt readability a little.

I know DRY is guideline, not an absolute rule. But in general, are you willing to hurt readability in the name of DRY?

解决方案

Refactoring: Improving the Design of Existing Code

The Rule of Three

The first time you do something, you just do it. The second time you do
something similar, you wince at the duplication, but you do the duplicate
thing anyway. The third time you do something similar, you refactor.

Three strikes and you refactor.


Coders at Work

Seibel: So for each of these XII calls you're writing an implementation.
Did you ever find that you were accumulating lots of bits of very similar code?

Zawinski: Oh, yeah, definitely. Usually by the second or third time you've cut and pasted
that piece of code it's like, alright, time to stop cutting and pasting and put it in a subroutine.

这篇关于你允许多少重复的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆