dotnet / try-samples

Creative Commons Attribution 4.0 International
1.09k stars 623 forks source link

Pattern matching not completed #58

Open DickBaker opened 4 years ago

DickBaker commented 4 years ago

[cf. https://www.youtube.com/watch?v=aUbXGs7YTGo&lc=UgzSxrWBQvVlwJ_E3Zp4AaABAg]

Surprisingly nobody (at NDC or in YT comments) has challenged Bill who at offset 44:48 & 45:40 declared victory with this solution public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) => (IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch { (true, TimeBand.MorningRush, true) => 2.00m, (true, TimeBand.EveningRush, false) => 2.00m, (true, TimeBand.MorningRush, false) => 1.00m, (true, TimeBand.EveningRush, true) => 1.00m, (true, TimeBand.Daytime, ) => 1.50m, (true, TimeBand.Overnight, ) => 0.75m, (false, , ) => 1.00m, }; and the MS docs show very similar at https://github.com/dotnet/try-samples/blob/master/csharp8/patterns-peakpricing.md

For goodness sake please refactor the explicit special conditions first and then drop through to the simple default 1.0m result public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) => (IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch { (true, TimeBand.MorningRush, true) => 2.00m, (true, TimeBand.EveningRush, false) => 2.00m, (true, TimeBand.Daytime, ) => 1.50m, (true, TimeBand.Overnight, ) => 0.75m, (, , _) => 1.00m }; which matches the special cases and dangling default (else) acts as catch-all for the rest. This distils the spaghetti 50 lines to an even simpler (and clearer) conclusion.

Fortunately the actual PeakTimePremium code at https://github.com/dotnet/try-samples/blob/master/csharp8/ExploreCsharpEight/Patterns.cs has now advanced

P.S. Don't declare victory too early (didn't we learn that from Gulf War-1?) PPS see attached graphic for those who think better visually WagnerVille.xlsx