I'm frustrated with how the Style/FetchEnvVar
cop works. It currently encourages replacing ENV["FOO"]
with ENV.fetch("FOO", nil)
, but this replacement doesn’t offer any real advantage. Using fetch
with nil
as the fallback essentially mimics the behavior of ENV["FOO"]
, while losing readability and not leveraging the main benefit of fetch
, which is to explicitly handle missing keys.
I’d like to propose that the cop be smarter about when it suggests using ENV.fetch
. Specifically, it should avoid autocorrecting or flagging ENV["FOO"]
usages unless a meaningful default value is being provided (i.e., something other than nil
). If the intention is to allow for a fallback value, fetch
makes sense; otherwise, it does not add value and may even reduce code clarity.
One alternative is to disable the cop entirely, but that removes any enforcement for meaningful uses of ENV.fetch
. Another idea is to allow configuration of acceptable default values or provide an option to ignore nil
as a second argument.
The current autocorrect suggestion creates code like ENV.fetch("FOO", nil)
which is functionally identical to ENV["FOO"]
, but more verbose and arguably harder to read. This reduces developer confidence in the value provided by the cop.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4