-
Notifications
You must be signed in to change notification settings - Fork 105
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
Add style for constants? #235
Comments
In typical R interactive sessions, all variables declared by the user reside in the global environment, making it unnecessary to style them differently based on namespaces. The concept of constant variables in R is meaningful only within environments. Attempting to enforce constants through naming conventions is inefficient. The R project provides specific documentation on namespacing in packages. Variables declared within package namespaces are sealed and should not be modified. 1 Assigning variables to the global environment from within functions is considered bad practice. 2 If your variable is a data frame or any form of data inside a package, use the data mechanisms and document it accordingly. 3 When writing functions that interact with variables exported from other packages or within their namespaces, namespace them appropriately (e.g., Consider using explicit environments if you need to maintain the state within your functions. 4 Footnotes |
I agree with your points @luciorq, perhaps I was unclear in my original comment as I think I'm thinking of a much more restricted meaning of constants. I am referring to variables that are exported by a package and can be accessed via the package namespace (but for the purpose of this discussion styling would also apply to internal constants within a package). The exported constant cannot be modified e.g., base::pi = 3
#> Error in base::pi = 3: object 'base' not found Created on 2024-11-15 with reprex v2.1.0 The aspect of styling is mainly to differentiate between functions and variables/constants exported by a package, or in the case of internal constants to help see when a piece of code is using a constant (as opposed to a variable defined elsewhere or input into a function). As this is a relatively uncommon feature of R this issue can be closed if deemed irrelevant. |
e = baseenv()
unlockBinding("pi", e)
assign("pi", exp(1), e)
lockBinding("pi", e)
base::pi
# [1] 2.718282 🙃 |
I was referring to modification by direct assignment, but wasn't familiar with If the argument is variables (or referred to in my previous comments as constants) can always be modified by some mechanism in R and therefore they shouldn't be styled, this is a valid viewpoint, although I still view styling them as helpful in my own experience. Either way documenting the style or lack thereof could be beneficial to package developers. |
Although fairly uncommon in R, internal and exported constants are used in R packages (and base R e.g.
?pi
). Would it be beneficial to add a short piece of advice on how to style these?I've had a very quick look at {lintr} and couldn't see anything on this, but definitely could have missed it.
PEP8 advises all caps if interested in what other language style guides say.
The text was updated successfully, but these errors were encountered: