Knowing and understanding the four visibility modifiers of Solidity is a base requirement for any Solidity developer.
These modifiers are:
1. external 2. public 3. internal 4. private
And each has its own use case.
A thread.
1. external
Only functions can be marked external.
External functions are part of the contract interface and can be called from other contracts and transactions.
They can't be called internally.
This leads to external functions being cheaper to execute.
1/14
Arguments of external functions can be directly read from the calldata.
They don't need to be copied over to memory like for public functions.
2/14
The reason is that internal calls are executed through jumps in the code (under the hood). Array arguments are internally passed as memory pointers.
Because of this, internal calls always need all variables in memory.
3/14
Best practice: Always use external when you don't want to call the function from within the same contract.
This saves gas.
4/14
2. public
Both state variables (the 'properties' of your contract) and functions can be marked as public.
Public state variables and functions can both be accessed from the outside and the inside.
5/14
The Solidity compiler automatically creates a getter function for public state variables.
For a public state variable myVar, the compiler generates an automatic getter function myVar() that returns the value of myVar.
6/14
When myVar() is called from within the same contract, myVar is actually accessed.
If accessed externally, the function is evaluated.
7/14
Best practice: Use public for publicly accessible state variables and when you want functions accessible from the outside and inside.
8/14
3. internal
internal is the default visibility for state variables.
Internal functions and state variables can both be accessed from within the same contract and in deriving contracts.
They aren't accessible from the outside.
9/14
Best practice: Use internal whenever you think state variables and functions should also be accessible in deriving contracts.
10/14
4. private
Private is the most restrictive visibility.
State variables and functions marked as private are only visible and accessible in the same contract.
11/14
Important: Private is only a code-level visibility modifier.
State marked as private is still visible to observers of the blockchain. It is just not accessible for other contracts.
12/14
Best practice:
Use private when you really want to protect your state variables and functions because you hide them behind logic executed through internal or public functions.
13/14
5. Thread end
You've made it to the end of this thread. Thank you for taking the time to read it!
If you liked this thread, leave a like, retweet the first tweet, it would mean the world to me! 💛
If you want more content like this, follow me for more.
14/14
• • •
Missing some Tweet in this thread? You can try to
force a refresh
I've implemented nearly a hundred smart contracts with Solidity during my short blockchain career, but I'm ready to drop Solidity right now if I think about WebAssembly on the blockchain.
Interested to learn more? Let's take a look at what WASM can do for blockchains!
🧵👇🏻
1/
Before we jump in:
If you don't exactly know what WebAssembly is, this thread might help you understand the basics first. 👇🏻