Skip to content

Refine Style/FetchEnvVar to preserve readability and intention #14165

@wafcio

Description

@wafcio

Is your feature request related to a problem? Please describe.

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.

Describe the solution you'd like

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.

Describe alternatives you've considered

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.

Additional context

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy