7

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?

1
  • 1
    Nothing 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. Aug 13, 2018 at 16:43

2 Answers 2

17

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.

2
  • Does this work for solidity 0.6.2?
    – True
    Sep 6, 2020 at 14:46
  • yup. Its simply a convention ^ Mar 10, 2022 at 8:38
2

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.

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.