2

For example:

    function contains(address _key) public view returns (bool) {
    return map[_key] != 0;
}

I don't know how this could be manipulated to change the state of anything, so to me it seems like it's obviously a view function. Therefore, is it necessary/required to specify it?

Thanks in advance!

1 Answer 1

2

It's not strictly necessary to specify that a function is a view function if it's already obvious from the function's code that it only reads data from the blockchain and doesn't modify it. However, it's a good practice to explicitly mark a function as a view function as it makes the intent of the function clearer and easier to understand for anyone reading the code.

Furthermore, marking a function as a view function has some practical benefits. For example, it allows the compiler to perform additional checks on the function's code to ensure that it really is a view function and doesn't modify state. It also allows other tools, such as web3.js or ethers.js, to optimize their interactions with the blockchain when calling view functions, as they know that they don't need to wait for the transaction to be mined and can instead return the result immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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