Why is it better to have 100 functions operate on one data structure than 10 functions on 10 data structure

半世苍凉 提交于 2019-12-03 01:08:09

问题


I have seen this quoted in a lot of places:

"It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." —Alan Perlis

But I have never seen it explained why this should be true. Is it just the idea that you should try to derive the other 9 data structures from the first to avoid duplicating the data? I feel like I'm missing some context.


回答1:


The quote is from Alan Perlis' Epigrams on Programming, which was published in 1982.

The meaning of this quote is embodied well in Lisp, where there are multitudes of functions that operate and deal specifically with lists, and you could accomplish a lot just with lists and the assortment of functions that operate on lists, which makes them much more powerful than any single-purpose data structure.

Lua, as another example, uses tables to simulate classes. Why use a table to make objects instead of creating language-level classes and objects like object-oriented languages do? Since your object is a table now, you can use any number of the functions defined for tables on your object, free of charge! Better yet, we didn't have to clutter the language with class-specific syntax and have to redefine functions from table that we want for our class.

What Perlis said is definitely a prominent mode of thought in Lisp and in functional programming in general. Those 100 functions on your one data structure can be composed together in lots of unique ways, since they all operate on the same data structure, but you can't really mix the 10 functions on 10 data structures as well, since they were defined only to work on their particular data structure.

A more modern and simpler variation of this is thinking in terms of abstractions. If we were coding in Java, would you rather write a 100 functions on the List interface, or the same set of ten functions, once for ArrayList, once for LinkedList, once for....




回答2:


Structure and Interpretation of Computer Programs (SICP) answers your question as below:

You can see the original content of the online version of the book here

EDIT (included from comment):

"In Pascal the plethora of declarable data structures induces a specialisation within functions." Specialisation is bad because it inhibits "serendipity"/creativity - I would say - with my own words.

In other words if functions are too special then they cannot be reused in ways that were not known at the time of the function creation.

Nice example is fold (https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Foldable.html) which is a data structure agnostic, general higher order function. It can be used on a tree, for example

data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a).


来源:https://stackoverflow.com/questions/6016271/why-is-it-better-to-have-100-functions-operate-on-one-data-structure-than-10-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!