Skip to content

Prefer Nullable<T>.GetValueOrDefault() over manual checking #33792

Closed

Description

@terrajobst

After checking a Nullable<T>.HasValue, it's common to see calls to Nullable<T>.Value; instead of calling Value, it's less work to call GetValueOrDefault(), as Value repeats the HasValue check. It's possible a future JIT could optimize away the duplicate check, but if nothing else using GetValueOrDefault() makes the job of the JIT easier.

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. GSPP commented on Jun 25, 2020

    @GSPP

    This is a high-noise analyzer. GetValueOrDefault is preferable from a performance standpoint, but this does not matter for 99% of the code.

    From a style perspective, Value is preferable because semantically is more correct than GetValueOrDefault. You never want a default. If anything, you want the explicit assertion in Value.

    If many high-noise analyzers like this one are default-on then people will learn that the analyzer suite is not meaningful to their work.

  8. stephentoub commented on Jun 25, 2020

    @stephentoub
    Member

    If many high-noise analyzers like this one are default-on then people will learn that the analyzer suite is not meaningful to their work.

    It wouldn't be on by default.

  9. xtqqczze commented on Nov 30, 2020

    @xtqqczze
    Contributor

    There is an existing code quality issue for Nullable<T>.Value following Nullable<T>.HasValue (#13699).

  10. iSazonov commented on Dec 2, 2020

    @iSazonov
    Contributor

    What about correlation with IDE0030: UseCoalesceExpressionForNullable?
    The rule suggest x ?? y instead of x != null ? x : y when x and y are nullable value types or nullable reference types.
    For the new analyzer I'd prefer x.GetValueOrDefault(y) for value types - this clearly indicates that the type is value, which adds readability to the code. In the case IDE0030 should be changed to work only for reference types.

  11. carlossanlop commented on Mar 2, 2021

    @carlossanlop
    Contributor

    @xtqqczze the issue you linked doesn't seem like it's going to affect this analyzer.
    @iSazonov we can keep offering IDE0030 alongside this new fixer.


    Suggested severity: Info
    Milestone: Future

    int? x = null;
    bool? b = null;
    
    
    // Before: The else has the exact same code as in the if body, but instead of x.Value it uses y, and is used only once
    int p;
    if (x.HasValue)
    {
        p = x.Value;
    }
    else
    {
        p = 5;
    }
    // After
    p = x.GetValueOrDefault(5);
    
    
    
    // Before: There is no else
    if (b.HasValue && b.Value)
    {
        DoSomething();
    }
    // After
    if (b.GetValueOrDefault())
    {
         DoSomething();
    }
    
    
    
    // Before: The else has the exact same code as in the if body, but instead of x.Value it uses y, and is used only once
    int y = 3;
    if (x.HasValue)
    {
        Console.WriteLine(x.Value);
    }
    else
    {
        Console.WriteLine(y);
    }
    // After
    Console.WriteLine(x.GetValueOrDefault(y));
    
    
    
    int e;
    // Before: A case where "IDE0030: Coalesce expression" will be offered
    e = x.HasValue ? x.Value : y;
    // After: We continue offering the IDE0030 fixer
    e = x ?? y;
    // But we also offer our new fixer
    e = x.GetValueOrDefault(y);
    
    
    
    // Before: Difficult case (optional), where Nullable<T> is used in multiple places in the if and else, but the body is the same
    int w = 5;
    if (x.HasValue)
    {
        Console.WriteLine(x.Value);
        Foo(x.Value);
        Bar();
    }
    else
    {
        Console.WriteLine(w);
        Foo(w);
        Bar();
    }
    // After
    int z = x.GetValueOrDefault(w);
    Console.WriteLine(z);
    Foo(z);
    Bar();
    
    
    
    
    // Code inside if/else is different
    if (x.HasValue)
    {
        Console.WriteLine(x.Value);
    }
    else
    {
        x = 5;
        Foo(x.Value);
    }
    // After
    if (x.HasValue)
    {
        Console.WriteLine(x.GetValueOrDefault());
    }
    else
    {
        x = 5;
        Foo(x.Value);
    }
  12. added
    api-ready-for-review
    and removed
    api-suggestion
    on Mar 2, 2021
  13. bartonjs commented on Jul 27, 2021

    @bartonjs
    Member

    Video

    The feeling in the room during API review is we should just fix the JIT here. The resulting code this suggests looks rather unnatural.

  14. xtqqczze commented on Aug 21, 2021

    @xtqqczze
    Contributor

    This issue has been fixed in the JIT in .NET 6, see #13699 (comment).

  15. ghost locked as resolved and limited conversation to collaborators on Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewarea-System.Runtimecode-analyzercode-fixer

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions