From 7b350a8c017e436ddb6790aa1acf20d9bd0ddeaa Mon Sep 17 00:00:00 2001 From: Ross Healy Date: Sun, 4 Feb 2024 00:10:44 +0000 Subject: [PATCH] Implemented ternary expression to make this if-else more concise. --- lottery-co-uk-scraper/Lotto.cs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lottery-co-uk-scraper/Lotto.cs b/lottery-co-uk-scraper/Lotto.cs index dca8a7e..1e38177 100644 --- a/lottery-co-uk-scraper/Lotto.cs +++ b/lottery-co-uk-scraper/Lotto.cs @@ -46,24 +46,16 @@ namespace lottery_co_uk_scraper var ballsDrawn = doc.DocumentNode.Descendants("span") .FirstOrDefault(x => x.Attributes["id"] != null && x.Attributes["id"].Value == "ballsDrawn"); - List? lottoBalls = null; - List? lottoBonusBalls = null; + List? lottoBalls = (ballsDrawn != null) + ? ExtractBalls(ballsDrawn, "lotto-ball") + : null; - if (ballsDrawn != null) - { - lottoBalls = ExtractBalls(ballsDrawn, "lotto-ball"); - lottoBonusBalls = ExtractBalls(ballsDrawn, "lotto-bonus-ball"); - } + List? lottoBonusBalls = (ballsDrawn != null) + ? ExtractBalls(ballsDrawn, "lotto-bonus-ball") + : null; - if (lottoBalls != null) - { - Console.WriteLine("Numbers inside lotto ball result: " + string.Join(", ", lottoBalls)); - } - - if (lottoBonusBalls != null) - { - Console.WriteLine("Numbers inside lotto bonus ball result: " + string.Join(", ", lottoBonusBalls)); - } + PrintResults("Numbers inside lotto ball result", lottoBalls); + PrintResults("Numbers inside lotto bonus ball result", lottoBonusBalls); var ballSetUsed = doc.DocumentNode.Descendants("td") .Where(x => x.InnerHtml.Contains("Ball Set Used:")) @@ -91,6 +83,7 @@ namespace lottery_co_uk_scraper } Console.WriteLine($"Failed to parse {className} value: {x.InnerText}"); + return 0; }) .ToList(); @@ -236,5 +229,13 @@ namespace lottery_co_uk_scraper Console.WriteLine("Rolldown: " + rolldown); } } + + private static void PrintResults(string label, List? values) + { + if (values != null) + { + Console.WriteLine($"{label}: " + string.Join(", ", values)); + } + } } } \ No newline at end of file