复制粘贴代码曾经被接受吗? [英] Is copy-and-paste coding ever acceptable?

查看:92
本文介绍了复制粘贴代码曾经被接受吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制和粘贴编程是一个坏主意,这是公认的,但是处理具有以下两个功能或代码块的情况的最佳方法是什么?仅有几种方法可以使它们泛滥成灾?

It's generally accepted that copy and paste programming is a bad idea, but what is the best way to handle a situation where you have two functions or blocks of code that really do need to be different in just a few ways make generalizing them extremely messy?

如果代码基本相同,除了一些细微的变化,该怎么办呢?但是,这些细微的变化并不容易通过添加参数,模板方法或类似的东西?

What if the code is substantially the same, except for a few minor variations, but those few minor variations aren't in things that are easy to factor out through either adding a parameter, template methods, or something like that?

更一般地说,您是否曾经遇到过这样的情况,您会承认确实有一点复制粘贴代码是真正的

More generally, have you ever encountered a situation where you would admit that a little copy-and-paste coding was truly justified.

推荐答案

我听说人们说遗嘱将复制并粘贴一次(将代码重复最多限制在两个实例中) ,因为除非您在三个或三个以上的地方使用代码,否则抽象不会奏效。 ()我本人,一看到需要就设法使它成为重构的好习惯。

I've heard people say the will copy and paste once (limiting duplication of code to at most two instances), as abstractions don't pay off unless you use the code in three places or more. () Myself, I try to make it a good habit of refactoring as soon as I see the need.

这篇关于复制粘贴代码曾经被接受吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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