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

前端 未结 2 1972
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 03:19

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.\"

相关标签:
2条回答
  • 2021-01-30 03:50

    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....

    0 讨论(0)
  • 2021-01-30 03:50

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

    Screenshot from SICP

    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).
    
    0 讨论(0)
提交回复
热议问题