Category: <span>C#</span>

The problem Whilst I have mostly got the hang of async/await, I still find unexplored corners of the subject that catch me out. I came across one the other day that was (in retrospect) so obvious that I’m surprised it never caught me out until then. This blog post is an attempt to explain the problem and solution, so that when I want to remember the hows and whys in a few months, I will have something to read! As my regular reader will know, I am not under any illusions that anyone else reads my blog, nor that anyone else find it useful. I use it as a place to make notes of things that I’m likely to want to find again. If it helps anyone else, well that’s just a bonus! The actual background to the use case is irrelevant, so I’ll illustrate with a trivial example. Suppose…

I’ve been trying to write functional code for some time now, and still have the feeling that I’m not doing it correctly, especially when seemingly simple things don’t work. Having bumped into one specific issue over and over again, I decided to try and sort it out once and for all. Imagine a stupidly simple API endpoint that take a float, and returns the square root doubled. I have helper functions as follows… My API endpoint returns a string of the form “Result: 23.3” if successful, or an error if not. I wire these together as follows… This works as expected. Obviously, my real code would have more from clauses than this, but I’m trying to keep this simple. Now, suppose (which is more realistic) the helper functions were async, then I would need to modify my endpoint to look like this… Again, all works. However, now in the case…

The “with” keyword is a crucial part of a functional programmer’s toolbox, but is sadly lacking in C#. In this blog post, I document the development of a template that will automatically generate a With() method for C# classes.

This entry is part 7 of 11 in the series Mazes For Programmers

As mentioned in the previous post in this series, I decided not to convert his Ruby code into C#, but rather to read his description of the algorithm, then try and write my own code. This way I am forced to understand the algorithm, which is far more productive than simply copying out someone else’s code without really knowing what it does. I had actually seen his code for Aldous-Broder, so had a basic idea of how he went about it, but hadn’t looked at it very closely, so was mainly writing it myself. The end result was actually very similar to his. One thing I discovered when running it was that my mazes were anything other than non-biased. Pretty much every maze I generated had an empty corridor right across the top row, leading me to realise that I had done something wrong. Furthermore, every now and then, the…

One of our main applications involves interacting with a third-party API. Whilst this is generally stable, we have had cases recently where it was throwing random exceptions. We could make the same request with the same parameters multiple times, and sometimes it would work, and sometimes it wouldn’t.

While we left the owners of the third-party API scratching their heads, we realised that the way we handled interaction with such APIs wasn’t robust enough. The first (and most obvious) improvement would simply be to try again.

As this is the sort of thing we’d want to do in a number of places, rather than repeat the same boilerplate code over and over again, we looked for a helper class.