Welcome to the landmark tenth edition of The Catch Block!

Text of Time #10
Photo by HENCE THE BOOM / Unsplash

In this edition: succinctness, LINQ, VS Code, Senior Developers, ECMAScript 2020, DNS, Westworld, and asynchronous.

Cool Reads

Writing More Succinct C# (Dan Clarke) -  I went into this post with a heavy dose of skepticism, because whenever I see title like "write more succinct code" that usually translate to "write code that's much harder to read but looks cool if you don't have to maintain it." But this post blew me away, and that's why it's the only post in this section this week. His "fabricated" example (the very first screenshot of code) is undoubtedly contrived, but also clearly explains why he's writing this post and what we, as readers, are going to get out of it.

Here's that main example. He starts with this code:

public IEnumerable<ItemResult> GetItemResults(bool someCondition)
{
    var items = _itemRepository.GetItems();
    
    var results = new List<ItemResult>();
    
    foreach(var item in items)
    {
        var newItem = new ItemResult();
        
        newItem.SomeField = item.SomeField;
        newItem.SomeOtherField = item.SomeOtherField;
        
        if(someCondition)
        {
            newItem.YetAnotherField = item.YetAnotherFieldV1;
        }
        else
        {
            newItem.YetAnotherField = item.YetAnotherFieldV2;
        }
        
        results.Add(newItem);
    }
    
    return results;
}

Perfectly readable, in many ways, but rather long-winded. All of that can be reduced, using some judicious LINQ and a ternary operator (?:).

public IEnumerable<ItemResult> GetItemResults(bool someCondition) =>
    _itemRepository.GetItems()
        .Select(x => new ItemResult
        {
            SomeField = x.SomeField,
            SomeOtherField = x.SomeOtherField,
            YetAnotherField = someCondition
                ? x.YetAnotherFieldV1
                : x.YetAnotherFieldV2
        }).ToList();

Looks quite a bit better, yes?

To tell the truth, I'm still a little wary of posts like this. I firmly believe that "readability" is a function of the kinds of assumptions we as programmers have about how code should be structured and organized, and those assumptions vary greatly from person to person. I would understand if some people actually found the more explicit, less condensed first version of this code more readable than the second version. That said, I'm all for moving toward cleaner code, so I will be looking for ways to implement the ideas that Dan discusses in this post in my own code.

Seriously, read this post! You'll be glad you did.

Previews and Announcements

Other Quality Reads

There were a LOT of good blog posts published this past week!

Catch Up with the Previous Issue!

The Catch Block #9 - CSS, CDNs, Pretty Flowers, Candy, and a Build
Pretty flowers, unity, board games, CSS Functions, candy, Clone Wars, and C# books.

Thanks for reading, and we'll see you next week!