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

前端 未结 2 1971
佛祖请我去吃肉
佛祖请我去吃肉 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

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

提交回复
热议问题