Why is “copy and paste” of code dangerous? [closed

2019-01-02 14:47发布

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?

18条回答
余欢
2楼-- · 2019-01-02 15:17

The obvious reason is that you take on a 'debt' for the future: any change you ever need to make in the code (not just bugfixes, any change) will now be twice as expensive to do because you have to update two places - and more risky because you WILL forget one of them eventually. In other words, making it work faster now will make your work even slower in the future, which can be good business sense but usually isn't.

But the more important reason is that the assumption "this is the same as that" is more often than not subtly wrong. Whenever your code depends on unspoken assumptions to be correct, copying it into another place results in errors unless these assumptions also hold in the new place. Therefore, the pasted code is often wrong from the start and not just after the next change.

查看更多
旧时光的记忆
3楼-- · 2019-01-02 15:18

copy and pasting is a disaster waiting to happen. Your boss should evaluate the price of shipping early with respect to the price of having broken code shipped to the end-user very soon.

查看更多
有味是清欢
4楼-- · 2019-01-02 15:18

There are trade-offs between speed of development of the immediate functionality in front of you (especially when the application is small), and longer term maintenance costs as the application grows.

Copy and paste is quicker for the immediate functionality, but will costs you dearly as the application grows in size, in terms of fixing bugs and making system wide changes and maintaining workflows between different components of the application.

That is the argument that business owners need to hear. It is similar to the accepted costs of maintaining a fleet of vehicles, however, with software, the broken aspects of the software architecture are generally hidden to the business side, and can only be seen by developers.

查看更多
孤独寂梦人
5楼-- · 2019-01-02 15:19

Tell your boss that the part of the each and every variable name includes the name of the old project and now you have to change them all, manually. If your boss doesn't know (or wants to know) why copy/paste is bad he/she might as well believe that :)

查看更多
孤独总比滥情好
6楼-- · 2019-01-02 15:21

The DRY principle (Don't Repeat Yourself): DRY on wikipedia.

"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

other link.

查看更多
妖精总统
7楼-- · 2019-01-02 15:21

in my company, we always work with classes and methods, and make technical documentation for them. I think its the best practice if u can use your own svn search aplications with good keys to find method class used before :)

查看更多
登录 后发表回答