Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conventions around storage var naming and use #10

Closed
milancermak opened this issue Apr 7, 2022 · 5 comments · Fixed by #22
Closed

Conventions around storage var naming and use #10

milancermak opened this issue Apr 7, 2022 · 5 comments · Fixed by #22

Comments

@milancermak
Copy link
Contributor

milancermak commented Apr 7, 2022

I think we should come up with a convention for naming and use of @storage_vars.

Here's an example what I've been using most recently:

@storage_var
func treasury() -> (address : felt):
end

@constructor
func constructor(treasury_ : felt):
    treasury.write(treasury_)
    return ()
end

@view
func get_treasury() -> (treasury : felt):
    let (treasury_) = treasury.read()
    return (treasury_)
end

@external
func set_treasury(treasury : felt):
    treasury.write(treasury_)
    return ()
end

That is, using a trailing underscore for the variable name inside a function (or as a argument passed into it) that holds the value of a @storage_var of the same name. But I don't really like it that much.

What I've seen in the code of others:

  1. using leading underscore for variable name: let (_treasury) = treasury.read()
  2. using trailing underscore for @storage_var name: let (treasury) = treasury_.read()
  3. using _storage appended to the name: let (treasury) = treasury_storage.read()

Is there any you like? Can you think of any other that you'd like us to adopt?

@milancermak
Copy link
Contributor Author

@nuts-rice @bllu404

@milancermak
Copy link
Contributor Author

Check out this discussion

It turns out that in Cairo, when importing from two files that hold a @storage_var of the same name, the one that's imported later will silently overshadow the former one. This is not good ⚠️

We ought to take that into account when coming up with a convention.

@nuts-rice
Copy link
Contributor

I like the leading underscore myself amd have been using that

@milancermak
Copy link
Contributor Author

I'm starting to dislike the trailing underscore because it can be confusing with a ignored var, like in let (q, _) = unsigned_div_rem(a, b) - here, q_ and q, _ are too similar.

@nuts-rice
Copy link
Contributor

I figure leading underscores would be best as well as _storage until there's clear syntactic conventions around types passed in as parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants