<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>C# on Ian Nelson</title><link>https://blog.iannelson.uk/tags/c/</link><description>Recent content in C# on Ian Nelson</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Sat, 05 Dec 2026 18:43:34 +0000</lastBuildDate><atom:link href="https://blog.iannelson.uk/tags/c/index.xml" rel="self" type="application/rss+xml"/><item><title>Who Reigns Supreme? Parsing Our WhatsApp Chat for Wordle Glory</title><link>https://blog.iannelson.uk/who-reigns-supreme-parsing-our-whatsapp-chat-for-wordle-glory/</link><pubDate>Tue, 31 Dec 2024 15:42:39 +0000</pubDate><guid>https://blog.iannelson.uk/who-reigns-supreme-parsing-our-whatsapp-chat-for-wordle-glory/</guid><description>&lt;p&gt;Back in 2022, in a bid to outwit my mother in our daily Wordle battles, I wrote some C# code to explore optimal starting guesses. &lt;a href="https://blog.iannelson.uk/the-best-wordle-starter-words/"&gt;You can read about that experiment here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Fast forward two years, and my Wordle addiction is alive and well. The daily challenge remains as compelling as ever, but it’s the family WhatsApp group – where scores are shared, victories boasted, and failures commiserated – that adds an extra layer of fun.&lt;/p&gt;</description><content:encoded><![CDATA[<p>Back in 2022, in a bid to outwit my mother in our daily Wordle battles, I wrote some C# code to explore optimal starting guesses. <a href="https://blog.iannelson.uk/the-best-wordle-starter-words/">You can read about that experiment here</a>.</p>
<p>Fast forward two years, and my Wordle addiction is alive and well. The daily challenge remains as compelling as ever, but it’s the family WhatsApp group – where scores are shared, victories boasted, and failures commiserated – that adds an extra layer of fun.</p>
<p>As 2024 winds down, I decided to dust off my coding skills and whip up a quick C# console application. The goal? To parse an export of our WhatsApp chat, gather all the shared Wordle scores, and find out if my strategies have paid off this year. Spoiler alert: they haven’t.</p>
<p>If you’ve got a similar Wordle-centric group chat with family or friends and fancy crowning your own Wordle Champion of the Year, the source code is available on GitHub: <a href="https://github.com/ianfnelson/WordleParser">WordleParser</a>.</p>
<h2 id="how-it-works">How It Works</h2>
<p>The application takes a single argument: the path to an exported chat text file. It parses the file using a <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L46">regular expression</a> to extract anything resembling a Wordle score. <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L27">Duplicate scores are weeded out</a>, and the <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L32">average family score for each calendar day is logged</a>. For those unfortunate days when a puzzle remains unsolved, the application <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L59">assigns a ‘seven’</a> to reflect the failure.</p>
<p>The app then aggregates scores into calendar <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L17">months</a> and <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L11">years</a>, <a href="https://github.com/ianfnelson/WordleParser/blob/main/WordleParser/WordleParser.cs#L76">calculating each family member’s stats for the chosen date range</a>. Participants are ranked based on their mean difference from the average family score per day.</p>
<p>For example, if three family members scored a 4, but one managed a dazzling 2, the day’s average would be 3.5. The three “fours” would each have a daily difference of +0.5, while the lucky “two” would boast a difference of -1.5.</p>
<h2 id="the-results">The Results</h2>
<p>So, who triumphed in the Nelson family Wordle-off this year? Did my painstaking analysis from 2022 finally give me the edge?</p>
<p>Reader, it did not.</p>
<p>Despite my best efforts, I only just managed to sneak into second place. Predictably, my mother reigned supreme, crushing us all with an average score of <strong>4.055</strong> and a mean difference from the family average of <strong>-0.128</strong>. This consistency made her our undisputed <strong>Wordle Family Champion of 2024</strong>.</p>
<p><img alt="Nelson Family Wordle Champion 2024" loading="lazy" src="https://images.iannelson.uk/2024/12/7DD41AB4-B664-4276-B1A1-3E9EA0F0DFB7-1.jpeg"></p>
]]></content:encoded></item><item><title>Back To Basics: On The Use And Abuse Of The Humble Boolean</title><link>https://blog.iannelson.uk/back-to-basics-on-the-use-and-abuse-of-the-humble-boolean/</link><pubDate>Tue, 01 Jul 2014 20:54:00 +0000</pubDate><guid>https://blog.iannelson.uk/back-to-basics-on-the-use-and-abuse-of-the-humble-boolean/</guid><description>&lt;blockquote&gt;
&lt;p&gt;“We who cut mere stones must always be envisioning cathedrals”&lt;/p&gt;
&lt;p&gt;— Stonecutter’s creed&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Consider for a moment, if you will, the humble Boolean. Taking only two possible values, it is the most basic of all the data types we programmers use, and its existence is fundamental to all the code we write. At the end of the day, when all is said and done, everything we do is just a big bunch of ones and zeroes.&lt;/p&gt;</description><content:encoded><![CDATA[<blockquote>
<p>“We who cut mere stones must always be envisioning cathedrals”</p>
<p>— Stonecutter’s creed</p>
</blockquote>
<p>Consider for a moment, if you will, the humble Boolean. Taking only two possible values, it is the most basic of all the data types we programmers use, and its existence is fundamental to all the code we write. At the end of the day, when all is said and done, everything we do is just a big bunch of ones and zeroes.</p>
<p>I assert that this warm familiarity often causes us to make use of booleans when we really shouldn’t. In particular, I want to call out two specific uses of the boolean which I now consider to be anti-patterns and avoid wherever possible:</p>
<h2 id="antipattern-1--methods-that-take-boolean-flag-parameters">Antipattern 1 – Methods that take boolean flag parameters</h2>
<p>We spend more time reading and interpreting code than we spend actually writing it. That line of code you’re absent-mindedly knocking out today will probably be read dozens of times by your colleagues and future developers. It behoves you to make your intentions as clear as possible at every opportunity. Ambiguity in code is a truly terrible thing. Don’t impede understanding of your intentions by using boolean parameters that at worst force the reader to navigate to the definition of the method being invoked, or at best require them to hover their mouse over the bool parameter to glimpse some intellisense insight.</p>
<p>An example – let’s take a look at a line of code which uses the NHibernate hbm2ddl tool to export the database schema:</p>
<p><code>new SchemaExport(cfg).Execute(false, true, false);</code></p>
<p>Quick – what do those trio of booleans do? It’s not remotely obvious and we are forced to consult the docs. How much nicer it would be if the API developers had opted instead to use a flags enumeration, similar to the RegexOptions enumeration in the System.Text.RegularExpressions namespace:</p>
<p><code>var regex = new Regex(@&quot;^file://&quot;, RegexOptions.Compiled | RegexOptions.IgnoreCase);</code></p>
<p>More often, I encounter boolean parameters in the wild where a developer has been charged with enhancing an existing method to behave slightly differently in some particular circumstance, leading to method calls along the lines of:</p>
<p><code>var user = CreateUser(email, password, true);</code></p>
<p>What does the final parameter do? Again we have to rely on intellisense or navigating to the source. It’s too easy to add the boolean parameter and slow down everyone who follows in our wake. We should take the time to implement the correct solution, whether that be as simple as a separate method with a more specific name, or a polymorphic family of suitably-named classes that implement the same interface. This kind of boolean parameter, quick as it is to add, not only slows us down but is often symptomatic of a deeper flaw in the class design.</p>
<p>Don’t even get me started on the optional parameters introduced in C# 4.0. It’s all too easy to invoke methods without noticing the existence of optional parameters with surprising defaults which can cause unexpected consequences. A sprinkling of syntactic sugar that can act as a catalyst for code decay.</p>
<blockquote>
<p>“Flag arguments are ugly. Passing a boolean into a function is a truly terrible practice. It immediately complicates the signature of the method, loudly proclaiming that this function does more than one thing. It does one thing if the flag is true and another if the flag is false!”</p>
<p>— Robert C. Martin, <a href="http://amzn.to/1lPJsXv">Clean Code</a></p>
</blockquote>
<p>As with many design principles, the visibility of the method is key in how problematic this code smell becomes. A boolean parameter on a private method, while a potential candidate for refactoring at a suitable time, is not the end of the world. But a boolean parameter on a public method, perhaps a member on a public interface, is a more pressing problem. Think very carefully before adding a boolean parameter to a method on a public API or other service boundary.</p>
<h2 id="antipattern-2--methods-that-unnecessarily-return-booleans">Antipattern 2 – Methods that (unnecessarily) return booleans</h2>
<p>OK, so I’ve explained that I don’t much like methods that take boolean parameters. What about methods that return them?</p>
<p>Well, I am an advocate of <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command-Query Separation</a>. Generally, if a method returns a bool I’m going to assume that it’s free of side effects and is answering a query, like this:</p>
<p><code>bool canOrder = CanPlaceOrder(customer, order);</code></p>
<p>Conversely, when methods actually <em>do</em> stuff (i.e. commands), I wouldn’t expect them to return anything at all, least of all a boolean.</p>
<p>OK, a couple of notable exceptions to this rule. I can just about cope with create/update methods returning an instance of the entity that has been saved, thus:</p>
<p><code>var user = CreateUser(email, password);</code></p>
<p>and I admit to having a soft spot for the TryParse pattern where we expect the happy path to succeed on most occasions:</p>
<p><code>if (int.TryParse(value, out accountId)) { ... }</code></p>
<p>but booleans returned from methods that perform commands? Well that’s just pointless and confusing. Consider the method signature:</p>
<p><code>public bool CreateUser(string email, string password)</code></p>
<p>Why return a bool? What does it mean? That the method call was successful? Then why bother mentioning it?<br>
If there is a problem of some kind (say, a user already exists with the specified email address), then throw a suitable exception with the necessary detail that I can choose to catch, log or allow to bubble up as I see fit. By returning a boolean, consumers of the method now need to both catch exceptions <em>and</em> inspect the return value (and what action would be appropriate anyway when faced with a context-free “false” return value?)</p>
<p>Command methods that return booleans feel like insecure children who need to boast of their every achievement. <strong>It’s OK to return void.</strong> Nobody will think any the less of you for simply doing what was asked and keeping quiet. Unix commands have been silently going about their business for years, and I feel that is an ethos that we developers should try to emulate. The era of the contrived return value that has to be dissected for meaning is long gone. Doing what was asked if possible, else throwing an appropriate exception is the twenty-first century way.</p>
]]></content:encoded></item><item><title>A C# Postcode Struct with Parser</title><link>https://blog.iannelson.uk/a-c-postcode-struct-with-parser/</link><pubDate>Tue, 29 May 2007 12:05:00 +0000</pubDate><guid>https://blog.iannelson.uk/a-c-postcode-struct-with-parser/</guid><description>&lt;p&gt;As &lt;a href="https://blog.iannelson.uk/postcode-validation/"&gt;discussed recently&lt;/a&gt;, I’ve been trying to knock together a class struct to represent a UK postcode, provide a means of parsing a string as &lt;em&gt;potentially&lt;/em&gt; being a valid postcode (or optionally just an outer code), and split that postcode into “outer” and “inner” code.&lt;/p&gt;
&lt;p&gt;I say &lt;em&gt;potentially&lt;/em&gt; because, as you’ll know if you’ve looked into this topic at all, it’s impossible to validate whether a string truly does represent a postcode without querying the (copyrighted and expensive-to-license) Postcode Address File. As a result, my solution is not very stringent – it is possible to persuade it to successfully parse an invalid postcode, but hopefully there should be no occasions when a valid postcode is rejected.&lt;/p&gt;</description><content:encoded><![CDATA[<p>As <a href="https://blog.iannelson.uk/postcode-validation/">discussed recently</a>, I’ve been trying to knock together a class struct to represent a UK postcode, provide a means of parsing a string as <em>potentially</em> being a valid postcode (or optionally just an outer code), and split that postcode into “outer” and “inner” code.</p>
<p>I say <em>potentially</em> because, as you’ll know if you’ve looked into this topic at all, it’s impossible to validate whether a string truly does represent a postcode without querying the (copyrighted and expensive-to-license) Postcode Address File. As a result, my solution is not very stringent – it is possible to persuade it to successfully parse an invalid postcode, but hopefully there should be no occasions when a valid postcode is rejected.</p>
<p>The code validates strings by checking for compliance with the standards defined in <a href="http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm">BS7666</a>, followed by checking for BFPO postcodes, and finally for a handful of notable exceptions (e.g. Girobank). If you know of any valid postcodes that are rejected by this routine, do let me know.</p>
<p>Oh, and incidentally, this was the first time I’ve used Visual Studio Orcas in anger to develop anything meatier than a Hello World. Although I wasn’t exactly pushing the technological envelope, I found the IDE to be pretty fast and stable, considering it’s a beta 1.</p>
<p>Source available on GitHub at <a href="https://github.com/ianfnelson/Postcode">https://github.com/ianfnelson/Postcode</a></p>
]]></content:encoded></item><item><title>Postcode Validation</title><link>https://blog.iannelson.uk/postcode-validation/</link><pubDate>Wed, 23 May 2007 13:05:00 +0000</pubDate><guid>https://blog.iannelson.uk/postcode-validation/</guid><description>&lt;p&gt;Wikipedia cracks me up sometimes. I’m sat here at my desk, trying to knock together some C# code to quickly validate an entered string as being a UK postcode. Naturally I turned to this &lt;a href="http://en.wikipedia.org/wiki/UK_postcodes"&gt;Wikipedia article&lt;/a&gt; for assistance, where I find this quip:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;Automatic validation of postcodes on the basis of pattern feasibility is therefore almost impossible to design, and the system contains no self-validating feature such as a check digit. &lt;strong&gt;Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.&lt;/strong&gt;&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;</description><content:encoded><![CDATA[<p>Wikipedia cracks me up sometimes. I’m sat here at my desk, trying to knock together some C# code to quickly validate an entered string as being a UK postcode. Naturally I turned to this <a href="http://en.wikipedia.org/wiki/UK_postcodes">Wikipedia article</a> for assistance, where I find this quip:</p>
<blockquote>
<p>&ldquo;Automatic validation of postcodes on the basis of pattern feasibility is therefore almost impossible to design, and the system contains no self-validating feature such as a check digit. <strong>Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.</strong>&rdquo;</p>
</blockquote>
<p>I think that’s what is known as a <a href="http://en.wikipedia.org/wiki/Long_running_transaction">long-running transaction</a>!!</p>
<p><strong>Update</strong> – the C# code that I wrote to handle this issue is available <a href="https://blog.iannelson.uk/a-c-postcode-struct-with-parser/">here</a>.</p>
]]></content:encoded></item><item><title>Big Integer</title><link>https://blog.iannelson.uk/big-integer/</link><pubDate>Mon, 17 Jul 2006 08:07:00 +0000</pubDate><guid>https://blog.iannelson.uk/big-integer/</guid><description>&lt;p&gt;As I mentioned &lt;a href="https://blog.iannelson.uk/project-euler/"&gt;last summer&lt;/a&gt;, I do enjoy whiling away a few hours attempting to solve the maths / progamming challenges set on &lt;a href="http://mathschallenge.net/index.php?section=project"&gt;Project Euler&lt;/a&gt;. My language of choice for most puzzles is C#, but this hampered me on some of the questions as there is no in-built support for really big integers.&lt;/p&gt;
&lt;p&gt;However, a little bit of Googling and I came across this excellent &lt;a href="http://www.codeproject.com/csharp/biginteger.asp"&gt;BigInteger class&lt;/a&gt; on Code Project which does everything I need and more – and it seems bloody fast, too. Thanks to this class, I’ve been able to knock off several more problems this weekend, and now have a rating of “11% Genius” – still some way to go, then…&lt;/p&gt;</description><content:encoded><![CDATA[<p>As I mentioned <a href="https://blog.iannelson.uk/project-euler/">last summer</a>, I do enjoy whiling away a few hours attempting to solve the maths / progamming challenges set on <a href="http://mathschallenge.net/index.php?section=project">Project Euler</a>. My language of choice for most puzzles is C#, but this hampered me on some of the questions as there is no in-built support for really big integers.</p>
<p>However, a little bit of Googling and I came across this excellent <a href="http://www.codeproject.com/csharp/biginteger.asp">BigInteger class</a> on Code Project which does everything I need and more – and it seems bloody fast, too. Thanks to this class, I’ve been able to knock off several more problems this weekend, and now have a rating of “11% Genius” – still some way to go, then…</p>
]]></content:encoded></item><item><title>Tech Books</title><link>https://blog.iannelson.uk/tech-books/</link><pubDate>Sat, 18 Jun 2005 21:06:00 +0000</pubDate><guid>https://blog.iannelson.uk/tech-books/</guid><description>&lt;p&gt;I succumbed to temptation and bought another book at the MSDN briefing yesterday, pausing only out of concern that the guy who mans the &lt;a href="http://www.computermanuals.co.uk"&gt;Computer Manuals&lt;/a&gt; stand at these events might think that I’m stalking him (or fancy him!). Having determined that I could hand over my Barclaycard whilst flashing my wedding ring, I brought my current reading list up to three:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.amazon.co.uk/exec/obidos/ASIN/078214361X"&gt;Developer to Designer: GUI Design for the Busy Developer&lt;/a&gt; – this was a wedding present from my mother-in-law and is proving to be an interesting, easy-going read so far. My competencies lie in the middle tier and database, and although I’m technically capable of making GUIs, I prefer to leave the design up to others. It’s inevitable though that most coders end up doing some design, and this book is, er, designed (sorry) to help with that process. The early section of the book concentrates on each UI element in turn, whilst the later part of the book looks at putting it all together, and has particular focus on the web – layout design patterns and interaction patterns. It’s a good read, making a nice contrast from the heavier books, and I’m pleased to learn that I’m not alone in hating Outlook’s labyrinthine Tools-&amp;gt;Options dialog!&lt;/p&gt;</description><content:encoded><![CDATA[<p>I succumbed to temptation and bought another book at the MSDN briefing yesterday, pausing only out of concern that the guy who mans the <a href="http://www.computermanuals.co.uk">Computer Manuals</a> stand at these events might think that I’m stalking him (or fancy him!). Having determined that I could hand over my Barclaycard whilst flashing my wedding ring, I brought my current reading list up to three:</p>
<p><a href="http://www.amazon.co.uk/exec/obidos/ASIN/078214361X">Developer to Designer: GUI Design for the Busy Developer</a> – this was a wedding present from my mother-in-law and is proving to be an interesting, easy-going read so far. My competencies lie in the middle tier and database, and although I’m technically capable of making GUIs, I prefer to leave the design up to others. It’s inevitable though that most coders end up doing some design, and this book is, er, designed (sorry) to help with that process. The early section of the book concentrates on each UI element in turn, whilst the later part of the book looks at putting it all together, and has particular focus on the web – layout design patterns and interaction patterns. It’s a good read, making a nice contrast from the heavier books, and I’m pleased to learn that I’m not alone in hating Outlook’s labyrinthine Tools-&gt;Options dialog!</p>
<p><a href="http://www.amazon.co.uk/exec/obidos/ASIN/0321245660">Effective C#: 50 Specific Ways to Improve Your C#</a> – another good read that isn’t too heavy, this is a good book of short hints and tips to dip into when waiting for the latest CTPs of VS and SQL Server to install (well, at least that’s when I’ve found myself reading it). Depending on your level of experience with C#, you may find some of these suggestions are teaching you to suck eggs, but they all come with detailed justifications that provide useful insights into .NET internals, and out of fifty points you’re bound to find something that you didn’t previously know. If you’re new to C#, get this book now and put it’s suggestions into practice from the start. If you think you’re an expert, borrow a copy and use it as a checklist of your knowledge.</p>
<p><a href="http://www.amazon.co.uk/exec/obidos/ASIN/0596007124">Head First Design Patterns</a> – this is the book I picked up yesterday (yes, partly because of the cute blonde on the cover), but it was the outstanding content that stopped me putting it down again. This doesn’t feel like a normal O’Reilly book, and it feels very far removed from the original Gang of Four’s Design Patterns book – yet it covers the same material. This is a patterns book that won’t send you to sleep, that will make you laugh, that will make those tricky concepts sink in and your misconceptions float away. It’s cool, clever, succinct and quite unlike any other tech book I’ve read. The only slight downer is that all the examples are in Java, but this really shouldn’t be an issue for any half-competent C# developer. Superb.</p>
]]></content:encoded></item><item><title>Property Access Modifiers in Whidbey</title><link>https://blog.iannelson.uk/property-access-modifiers-in-whidbey/</link><pubDate>Mon, 15 Nov 2004 19:11:00 +0000</pubDate><guid>https://blog.iannelson.uk/property-access-modifiers-in-whidbey/</guid><description>&lt;p&gt;Ooh, this is great news:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://nxopinion.robertsoninstitute.org/blogs/rcecil/archive/2004/11/11/167.aspx"&gt;http://nxopinion.robertsoninstitute.org/blogs/rcecil/archive/2004/11/11/167.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It’s a little thing, to be sure, but I often find myself wanting to make a setter internal or private while leaving the getter public. So, I’m happy. It doesn’t take much 🙂&lt;/p&gt;</description><content:encoded><![CDATA[<p>Ooh, this is great news:</p>
<p><a href="http://nxopinion.robertsoninstitute.org/blogs/rcecil/archive/2004/11/11/167.aspx">http://nxopinion.robertsoninstitute.org/blogs/rcecil/archive/2004/11/11/167.aspx</a></p>
<p>It’s a little thing, to be sure, but I often find myself wanting to make a setter internal or private while leaving the getter public. So, I’m happy. It doesn’t take much 🙂</p>
]]></content:encoded></item></channel></rss>