2

I know when to use view and pure, but I dont know why we use it. Does EVM optimize anything When I use them?

1

For the same reason that you need to declare access-level (public, protected, private) in any object-oriented language.

It is not mandatory, and your code will compile successfully if you use public everywhere.

But by using a lower access-level, you are "declaring your intentions" - to yourself, as well as to anyone else using your code (or API).

So, for example, your clients (users) will know to "ignore" any function or variable declared private.

In addition, if you ever change a specific function (or access a specific variable) in a manner which is not permitted by its access-level, then the compiler will shout at you and remind you that it wasn't your original intention to begin with (hence you should either rethink your change or redeclare your intention).

  • emmm, I get your mean, but does EVM really have no optimization about this keyword? – Fly May 7 '18 at 6:13
  • @Fly: No, it actually does, and it is specified in the Solidity docs if I remember correctly. – goodvibration May 7 '18 at 6:38
  • I only see when to use view in the docs, could you tell me where it is specified if you have time? – Fly May 7 '18 at 15:26
2

I can't speak for any EVM level optimizations, but in high-security oriented languages like Solidity, this is often done to mark a method as without-a-doubt non state changing, which makes auditing much easier and clearer to follow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.