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?

0

3 Answers 3

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.

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.

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!)

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.