你觉得是过度泛化? [英] What do you feel is over-generalization?

查看:159
本文介绍了你觉得是过度泛化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haskell和其他功能语言中花了一些时间,我来欣赏来自一般性描述问题的设计的简单性。虽然模板编程的许多方面可能不是很简单,但一些使用是常见的,我不认为它们是阻碍清晰度(特别是功能模板)。我发现模板通常可以简化当前设计,同时自动添加一点未来的电阻。为什么他们的功能被降级到图书馆作家?



另一方面,有些人似乎避免了像瘟疫这样的模板。我可以理解这十年前,通用类型的概念对于大多数编程社区来说是外国人。但现在所有流行的静态类型的OO语言支持一种形式或另一种形式的泛型。 [/ p>

最近,我向我表达了一种这样的保守态度:


您不应该做任何比必要的更一般的事情 - 软件开发的基本规则。


真的很惊讶地看到这样说,如此不屑一顾,好像它应该是不言而喻的。个人而言,我发现它远不是不言而喻,什么用语言像Haskell,其中一切都是通用的,除非你另有规定。也就是说,我想我明白这个观点来自哪里。



在我的脑海里,我确实有这样的规则。现在,它在最前沿,我意识到我一直在解释它的整体架构。例如,如果你有一个类,你不想加载它与你可能有一天使用吨的功能。如果你只需要一个具体的版本(虽然可模拟性可能是对这一个反驳)不要打扰接口。这样的事情...



然而,我不做的是在微观层面应用这个原则。如果我有一个小的效用函数,没有理由依赖于任何特定的类型,我会做一个模板。



那么你觉得怎么样?你认为是什么是过度泛化?这个规则根据上下文有不同的适用性吗?你甚至同意这是一条规则吗?

解决方案

过度泛化使我疯狂。我不害怕模板(无处不在),我喜欢一般的解决方案。但我也喜欢解决客户支付的问题。如果是一个星期的项目,为什么我现在资助一个月的盛会,这将继续工作不仅通过明显可能的未来变化,如新税,但可能通过发现新的月亮或火星上的生活?



将此回到模板,客户端请求一些能力,涉及到你写一个函数,接受一个字符串和一个数字。你给我一个模板化的解决方案,它采取任何两种类型,对我的具体情况正确的事情,在可能或不可能是正确的(由于没有要求)在其余的情况下,我不会感激。我会被打断的,除了支付你,我必须支付一个人来测试,有人来记录它,并有人在你的约束下,如果一个更一般的情况下应该发生的事情。



当然,并不是所有的泛化都是泛化。一切都应该尽可能简单,但不简单。一般的必要,但没有更一般。作为我们可以承受的测试,但没有更多的测试。等等,预测什么可能改变和封装它。所有这些规则都很简单,但不容易。这就是为什么智慧在开发人员和管理他们的人的重要性。


Having spent some time playing around in Haskell and other functional languages, I've come to appreciate the simplicity of design that comes from describing problems in general terms. While many aspects of template programming can be far from simple, some uses are common enough that I don't think they're an impediment to clarity (especially function templates). I find templates can often simplify the current design while automatically adding a bit of future-resistance. Why should their functionality be relegated to the library writers?

On the other hand, some people seem to avoid templates like the plague. I could understand this a decade ago when the very concept of generic types was foreign to much of the programming community. But now all of the popular statically-typed OO languages support generics of one form or another. The added familiarity seems to warrant an adjustment of the conservative attitudes.

One such conservative attitude was expressed to me recently:

You should never make anything more general than necessary - basic rule of software development.

I was quite honestly surprised to see this stated so dismissively as if it should've been self evident. Personally I find it far from self-evident, what with languages like Haskell where everything is generic unless you specify otherwise. That being said, I think I understand where this point of view comes from.

In the back of my mind, I do have something like that rule rattling around. Now that it's at the forefront, I realize I've always interpreted it in the light of overall architecture. For example, if you have a class, you don't want to load it up with tons of features you might one day use. Don't bother making interfaces if you only need one concrete version (though mockability might be a counterargument to this one). Things like that...

What I don't do, however, is apply this principle on the micro level. If I have a small utility function that has no reason to be dependent on any particular type, I'll make a template.

So what do you think, SO? What do you consider to be over-generalizing? Does this rule have differing applicability depending on the context? Do you even agree this is a rule?

解决方案

Over generalizing makes me crazy. I'm not scared of templates (nowhere near) and I like general solutions. But I also like solving the problem for which the client is paying. If it's a one week project, why am I now funding a one month extravaganza which will continue to work not only through obvious possible future changes like new taxes, but probably through the discovery of new moons or life on mars?

Bringing this back to templates, the client asks for some capability that involves your writing a function that takes a string and a number. You give me a templated solution that takes any two types and does the right thing for my specific case and something that might or might not be right (due the absence of requirements) in the rest of the cases, and I will not be grateful. I will be ticked off that in addition to paying you I have to pay someone to test it, someone to document it, and someone to work within your constraints in the future if a more general case should happen to come along.

Of course, not all generalization is over generalization. Everything should be as simple as possible, but no simpler. As general as necessary, but no more general. As tested as we can afford, but no more tested. Etc. Also, "predict what might change and encapsulate it." All these rules are simple, but not easy. That's why wisdom matters in developers and those who manage them.

这篇关于你觉得是过度泛化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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