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

Add style for constants? #235

Open
joshwlambert opened this issue Oct 31, 2024 · 4 comments
Open

Add style for constants? #235

joshwlambert opened this issue Oct 31, 2024 · 4 comments

Comments

@joshwlambert
Copy link

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.

@luciorq
Copy link

luciorq commented Nov 2, 2024

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., base::pi).

Consider using explicit environments if you need to maintain the state within your functions. 4

Footnotes

  1. https://cran.r-project.org/doc/manuals/R-exts.html#Package-namespaces-1

  2. https://design.tidyverse.org/spooky-action.html#what-precisely-is-a-spooky-action

  3. https://r-pkgs.org/data.html

  4. https://adv-r.hadley.nz/environments.html#explicit-envs

@joshwlambert
Copy link
Author

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.

@MichaelChirico
Copy link
Collaborator

The exported constant cannot be modified e.g.,

e = baseenv()
unlockBinding("pi", e)
assign("pi", exp(1), e)
lockBinding("pi", e)
base::pi
# [1] 2.718282

🙃

@joshwlambert
Copy link
Author

I was referring to modification by direct assignment, but wasn't familiar with unlockBinding() and lockBinding() so appreciate them being pointed out.

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.

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

No branches or pull requests

3 participants