8 ms·
I have recently came to like the following distinction: - Let data be relatively static, immutable, objects (just as the post suggests) - For functions which
by samuell 8y ago
I have recently came to like the following distinction:
- Let data be relatively static, immutable, objects (just as the post suggests)
- For functions which are merely read-only views of this data, implement them as methods on the class (An example would be "Volume()" on a data object representing a geometric object)
- For functions that changes the original data on data objects, implement them as separate functions (can be encapsulated in asynchronous black-box processes ala flow-based programming if you like), returning new, immutable instances of the objects they operate on.
This allows keeping data objects immutable, while still allowing you to gather certain frequently used calculations on the object itself, for code readability / maintainability, as long as they are read-only.