Skip to content

pattern check null first! #8251

Closed Answered by 333fred
DickBaker asked this question in Language Ideas
Jun 27, 2024 · 3 comments · 1 reply
Discussion options

You must be logged in to vote

Let me see if I'm understanding your point correctly; for this example, you're suggesting to move the null branch first?

public decimal CalculateDiscount(Order order) =>
    order switch
    {
        { Items: > 10, Cost: > 1000.00m } => 0.10m,
        { Items: > 5, Cost: > 500.00m } => 0.05m,
        { Cost: > 250.00m } => 0.02m,
        null => throw new ArgumentNullException(nameof(order), "Can't calculate discount on null order"),
        var someObject => 0m,
    };

If that's the case, then I have good news for you: you don't have to reorder anything here. Switch statements and expressions already handle this. The underlying logic is somewhat complicated (involving topological graph …

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@DickBaker
Comment options

Answer selected by DickBaker
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants