是“猴子修补”吗?真的那么糟糕? [英] Is "monkey patching" really that bad?

查看:161
本文介绍了是“猴子修补”吗?真的那么糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些语言(如Ruby和JavaScript)具有开放类,允许您修改偶数核心类的接口,如数字,字符串,数组等。显然,这样做可能会使熟悉API的其他人感到困惑但是有充分的理由否则,请假设您正在添加到界面而不更改现有行为?

Some languages like Ruby and JavaScript have open classes which allow you to modify interfaces of even core classes like numbers, strings, arrays, etc. Obviously doing so could confuse others who are familiar with the API but is there a good reason to avoid it otherwise, assuming that you are adding to the interface and not changing existing behavior?

例如,添加一个 Array.map 实现到未实现ECMAScript第5版的Web浏览器(如果您不需要全部的jQuery)。或者你的Ruby数组可能会受益于使用inject的sum方便方法。只要更改被隔离到您的系统(例如,不是您发布的软件包的一部分),是否有充分的理由不利用此语言功能?

For example, it might be nice to add a an Array.map implementation to web browsers which don't implement ECMAScript 5th edition (and if you don't need all of jQuery). Or your Ruby arrays might benefit from a "sum" convenience method which uses "inject". As long as the changes are isolated to your systems (e.g. not part of a software package you release for distribution) is there a good reason not to take advantage of this language feature?

推荐答案

维基百科对猴子修补的陷阱进行了简短的总结:

Wikipedia has a short summary of the pitfalls of monkey-patching:

http://en.wikipedia.org/wiki/Monkey_patch#Pitfalls

所有事情都有时间和地点,也适合猴子修补。经验丰富的开发人员可以掌握许多技术,并了解何时使用它们。它很少是一种技术本身就是邪恶的,只是不加思索地使用它。

There's a time and place for everything, also for monkey-patching. Experienced developers have many techniques up their sleeves and learn when to use them. It's seldom a technique per se that's "evil", just inconsiderate use of it.

这篇关于是“猴子修补”吗?真的那么糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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