25

I see contracts that use underscores in front of parameter names, like so:

function Greeter(string _greeting) public
{
   greeting = _greeting;
}

What's the purpose of these underscores? Do they have a similar meaning as 'internal use' names in Python?

J-B
  • 8,941
  • 16
  • 46
  • 77
Joël
  • 1,720
  • 2
  • 17
  • 35

3 Answers3

24

There is no semantic difference. It is a style used to differentiate between function arguments and global variables.

In this case, it differentiates between the global variable named greeting and the corresponding function parameter.

Tjaden Hess
  • 37,046
  • 10
  • 91
  • 118
16

In most situations the storage variable is going to be used in many more places throughout the contract's code. The function argument may show up in a few places, but in most cases it won't be as prevalent as the storage variable.

It is this developer's opinion that it is nicer to work with greeter than _greeter. In my code the function argument gets the underscore prefix since I work with it in fewer locations. This leaves the rest of my code free to use a normal variable rather than one that is prefixed.

A less opinionated reason would be that for public variables, using an underscore prefixed name would also mean that the getter function would have the same name. This results in a contract ABI in which some of the functions have underscore prefixes which may be confusing to users.

Piper Merriam
  • 3,592
  • 3
  • 22
  • 34
3

There is a recommendation from Oyente that

Allow _ at the beginning of the mixed_case match for private variables and unused parameters.

And says:

mixedCase (differs from CapitalizedWords by initial lowercase character!)

Ender
  • 265
  • 2
  • 6