Should I represent database data with immutable or mutable data structures?

后端 未结 3 2180
清歌不尽
清歌不尽 2021-02-13 23:13

I\'m currently programming in Scala, but I guess this applies to any functional programming language, or rather, any programming language that recommends immutability and can in

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-13 23:35

    Why is a database mutable? Is it a fundamental nature of databases to be mutable? The relational model and using it as a persistence store for your application data might steer you towards this conclusion, but it may not be a fundamental property.

    Given that you may have other options such as storing a new version of your data when you update it, perhaps the premise of the question is undermined somewhat. Perhaps, even if you do have a 'mutable' database, you still need to provide a new value for the update function that is separate from the old value – consider for instance an optimistic lock where the update should only occur if the old value has not in the meantime changed.

    In other words, the mutability or otherwise of the database should not matter at all, you are dealing with a separate domain layer in your application. If you need to ask then the answer will always be immutable. Mutability is a complexity vector that experts should only introduce as a performance optimisation when it has been demonstrated to be necessary.

提交回复
热议问题