I'm a beginner, just recently started reading the docs for Solidity.
I can't seem to find this anywhere on google. What does the underscore mean when placed with the indexed
keyword?
Examples:
_from
_id
_amount
_account
How are these different from other variables, and how are they used in events?
-
1Nothing special, just a coding convention. For example, you can decide to use it for every local local variable in your code. BTW, it is not recommended anywhere in the Solidity Style Guide.– goodvibrationCommented Aug 13, 2018 at 16:43
2 Answers
There's no meaning as far as Solidity is concerned.
Some programmers choose to use a leading underscore for all function parameters, just as a convention to indicate that they're function parameters.
They're also often used so as to avoid collisions, e.g.:
uint256 totalSupply;
constructor(uint256 _totalSupply) public {
totalSupply = _totalSupply;
}
There, calling the parameter totalSupply
would shadow the existing state variable totalSupply
, so the leading underscore is used to avoid the naming collision.
Just to add a little more context, as stated by someone else in the comments you won't find any mention of the leading underscore in the styling guide.
You can however find a recommendation to use a trailing underscore to avoid naming collision.
This convention is suggested when the desired name collides with that of an existing state variable, function, built-in or otherwise reserved name.
As to why it's being used in Solidity, as an example in Javascript the leading underscore is used as a convention to fake encapsulation and indicate that a specific name is protected.
In C# it indicates that a field name is private.
So I guess there's a general tendency to use the leading underscore to indicate that a name is reserved for internal use only.