Tuesday, 21 May 2024
Arianne Weekes
5 minute read
Most of us didn’t start learning our coding languages of choice as they were invented. We jumped in at the deep end after years or decades of changes have been built on top of each other. The first beta versions of .NET 1.0 were released back in 2000. Before the iPod!
Those learning established languages have the advantage of learning within a well-established community with never ending resources: design patterns, best practice guides, and Stack Overflow threads galore.
We have the honour of seeing a polished offering, but none of the journey which went into making it.
This can lead to learning over-simplified hacks and rules. Or learning implementation whilst skimming over the underlying concepts.
For example, here’s a snippet based on a bit of code I wrote before the course:
private async Task<IEnumerable<HttpContent>> MakeApiCalls()
{
List<HttpContent> result = new();
foreach (string uri in URIs)
{
HttpResponseMessage response = await httpClient.PostAsJsonAsync(uri, dto);
HttpContent message = response.Content;
result.Add(message);
}
return result;
}
The async experts amongst you will have spotted the POSTs are happening in sequence. One POST is made, the response comes back, the content is added to a list, and then the whole thing starts again.
When I wrote this, I knew just enough about asynchronous methods to make the red squiggles go away. Does it compile? Then it’s done.
But behold:
private async Task<IEnumerable<HttpContent>> MakeApiCalls()
{
List<Task<HttpContent>> calls = new();
foreach (string uri in URIs)
{
Task<HttpContent> task = httpClient.PostAsJsonAsync(uri, dto)
.ContinueWith(t => t.Result.Content);
calls.Add(task);
}
return await Task.WhenAll(calls);
}
This version sets up each POST so they can be made in parallel. In the foreach loop a task is being described which tells the compiler how to make the call but, crucially, doesn’t make it! Only after a complete list of tasks has been created are they told to run.
It uses WhenAll to take advantage of using an asynchronous method instead of working around it.
Even better, it can be easily refactored into one easy-to-read LINQ statement which could compile with an extra-efficient enumerator.
private async Task<IEnumerable<HttpContent>> MakeApiCalls()
{
IEnumerable<Task<HttpContent>> calls = URIs.Select(async uri =>
{
var response = await httpClient.PostAsJsonAsync(uri, dto);
return response.Content;
});
return await Task.WhenAll(calls);
}
Let me assume that if you don’t relate to this example, you do relate to the experience. That you’ve pushed code which has a ‘smell’ or has been ‘approved with suggestions’ which you didn’t understand so didn’t take on board.
After all, you don’t know what you don’t know.
Well. You could embrace AI, ask it to write code or improve any methods you feel unsure about. You wouldn’t learn the principles but if the code you ship improves you’ve technically solved the problem.
Or you could go back to the beginning. Learn C# 1.0 and go up from there. Never forget to put () in your parameter-less lambda expressions because you know what you had to type before lambda expressions were an option.
That would take a very long time, though. And it sounds boring.
It’s much faster to learn from someone who already did that hard work. Plenty of people got into these languages when they were the brand-new thing; some of them became trainers.
If you are already signed up for training or mentoring – ask why. Don’t just nod and apply the advice people give you. Ask why it’s better, and how they know.
If you frequently skip your personal development, do a random deep dive. Look for the last thing you touched in Microsoft docs or browse What’s new. Something you lost hope in might’ve changed a month ago.
Try out an Andrew Clymer and Richard Blewett training course for yourself – they are delivering Asynchronous Programming at SDD Conference in May.
Apply for a job with Rock Solid Knowledge and benefit from their expertise every day.
And remember, WhenAll else fails... it's not in an async method, why are you using it there?
Last updated: Tuesday, 21 May 2024
Software Developer
They/Them He/Him She/Her
We're proud to be a Certified B Corporation, meeting the highest standards of social and environmental impact.
+44 333 939 8119