Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain reasoning behind avoiding mutation #27

Open
pipermerriam opened this issue Oct 22, 2018 · 5 comments
Open

Explain reasoning behind avoiding mutation #27

pipermerriam opened this issue Oct 22, 2018 · 5 comments

Comments

@pipermerriam
Copy link
Member

We need a section which explains the reasoning behind why we avoid mutation in our code:

see: ethereum/py-evm#1421 (comment)

It would be really nice if it included code examples for non-mutative approaches to common patterns that are typically accomplished using mutation.

@cburgdorf
Copy link
Contributor

Here's my take ethereum/py-evm#1421 (comment)

@carver
Copy link
Contributor

carver commented Oct 23, 2018

Making the RLP objects immutable had some really cool effects, but I don't have it distilled down to a clear story right now. Fleshing that out could be a great case study into why we prefer immutable objects. I roughly recall benefits like:

  • caching the whole object becomes trivial
  • caching properties on the object becomes trivial (eg~ the hash of the encoded rlp)
  • cross-process sharing is trivial, with fewer surprises
  • I seem to remember catching at least one place where an RLP object was mutated by a downstream method and causing a subtle bug
  • probably more...

@cburgdorf
Copy link
Contributor

I seem to remember catching at least one place where an RLP object was mutated by a downstream method and causing a subtle bug

We can come up with a simple (artificial) code example that crashes at runtime because code place X has a reference to Y where Y qualified some condition at the time of verification but then crashes when it is being used later because the condition changed because of some mutation. Not real world code but just to give a code example that demonstrates how mutation imposes such risks.

@davesque
Copy link
Contributor

From a theoretical point of view, expressing things in terms of immutable objects makes it easier to relate the implementation of an algorithm to its mathematical definition. I believe mathematicians tend to avoid the use of mutability when formally defining algorithms.

@rayrapetyan
Copy link

A good example of when immutable objects can be a true evil: ethereum/trinity#182

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants