Skip to content

Don't guard Dictionary<K, V>.Remove(key) by ContainsKey(key) #33797

Closed

Description

@terrajobst

The pair can be combined into just the Remove call.

Category: Performance

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. added
    code-fixer
    on Mar 21, 2020
  5. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Medium
    • Fixer: Medium
  6. removed
    untriaged
    on Mar 21, 2020
  7. carlossanlop commented on Jan 8, 2021

    @carlossanlop
    Contributor

    Suggested severity: Info

    Example

    // Before
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                    data.Remove(key);
                // or
                if (data.ContainsKey(key))
                    data.Remove(key, out var value);
            }​​
    
    // After
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                data.Remove(key);
                // or
                data.Remove(key, out var value1);
            }​​

    Avoid triggering the analyzer if there's additional code inside the if statement:

            public void DontTrigger(Dictionary<string, int> data, string key, Dictionary<string, int> otherData)
            {​​
                if (data.ContainsKey(key))
                {
                    otherData[key] = data[key]; // Unrelated code
                    data.Remove(key);
                }
            }​​
  8. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jan 15, 2021
  9. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 4, 2021
  10. terrajobst commented on Feb 4, 2021

    @terrajobst
    ContributorAuthor

    Video

    • Makes sense.

    We could further improve the fixer to handle this as well:

    // Before
    if (data.ContainsKey(key))
    {
        data.Remove(key);
        SomeUnrelatedCode();
    }
    // After
    if (data.Remove(key))
    {
        SomeUnrelatedCode();
    }

    But it's not necessary -- we can start simple and take it from there.

  11. 2 remaining items

  12. carlossanlop commented on Feb 11, 2021

    @carlossanlop
    Contributor

    Thanks for your help, @chucker! I'm assigning the issue to you. Feel free to submit a PR whenever you get a chance, and we can take a look.

  13. removed
    help wanted
    on Feb 11, 2021
  14. chucker commented on Feb 11, 2021

    @chucker

    (The PR linked above does now include the fixer, too.)

  15. modified the milestones: Future, 7.0.0 on Aug 20, 2021
  16. paulomorgado commented on Sep 3, 2021

    @paulomorgado
    Contributor

    is there an issue like this for HashSet<T>?

    • Don't guard HashSet<T>.Remove(item) by HashSet<T>.Contains(item)?
    • Don't guard HashSet<T>.Add(item) by HashSet<T>.Contains(item)?
  17. carlossanlop commented on Sep 14, 2021

    @carlossanlop
    Contributor

    @paulomorgado if you can't find any issue, please create an API proposal.

  18. paulomorgado commented on Sep 15, 2021

    @paulomorgado
    Contributor

    An API proposal????

  19. chucker commented on Oct 13, 2021

    @chucker

    @paulomorgado yes — make a new issue over here

    See also these guidelines.

  20. added
    in-pr
    on Jul 6, 2022
  21. modified the milestones: 7.0.0, Future on Jul 9, 2022
  22. ghost locked as resolved and limited conversation to collaborators on Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Collectionscode-analyzercode-fixerin-pr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions