为什么要“复制并粘贴”代码危险吗? [英] Why is "copy and paste" of code dangerous?

查看:148
本文介绍了为什么要“复制并粘贴”代码危险吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我的老板会向我们抱怨:

Sometimes, my boss will complain to us:


为什么我们需要这么长时间才能实现某个功能?

Why do we need such a long time to implement a feature?

实际上,此功能之前已在其他应用程序中实现,您只需要从此处复制并粘贴代码。成本应该很低。

Actually, the feature has been implemented in another application before, you just need to copy and paste codes from there. The cost should be low.

这确实是一个难题,因为从我的角度来看,复制和粘贴代码并不是一件简单的事情。

It's really a hard question, because copy and paste codes is not such a simple thing in my point of view.

您是否有充分的理由向您的非技术老板解释这一点?

Do you have any good reasons to explain this to your non-technical boss?

推荐答案

如果您在复制粘贴代码中发现错误,则需要在每个位置修复该错误,并希望您能记住所有错误(对于更改的要求也是如此)。

If you find a bug in your copy-paste code, you will need to fix it every place you did and hope you can remember them all (this also holds for changed requirements).

如果将逻辑放在一个地方,则在需要时更容易进行更改(因此,如果您决定应用程序需要更新,则只需在一个地方进行)。

If you keep logic in one place, it is easier to change when needed (so if you decide that the application needs updating, you only do it in one place).

让老板了解 DRY原则(不要重复自己)。

Have your boss read about the DRY principle (Don't Repeat Yourself).

您所描述的内容听起来像是的完美用法,您可以在其中共享代码并将其保存在一个地方。

What you are describing sounds like the perfect use for libraries, where you share code and only keep it in one place.

如果我打算 r,我只会复制粘贴代码efactor 之后-确保我以后提取通用代码,以便可以重用尽可能多的逻辑。不久之后,我指的是几分钟和几个小时,而不是几天和几周。

I would only ever copy-paste code if I intended to refactor it soon after - making sure I later on extracted common code so I could reuse as much logic as possible. And by soon after, I mean minutes and hours later, not days and weeks.

这篇关于为什么要“复制并粘贴”代码危险吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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