Welcome to the 71st edition of The Catch Block!

A new kind of RGB. Photo by Alex Kondratiev / Unsplash

In this edition: keeping your tests updated; software as a product; getting property names from lambda expressions; resolving conflict in remote teams; real-world refactoring; and default browser problems in Windows 11.

Keep Your Tests Updated!

Yes, yes, we all know that testing is important and we should do it. But what about maintaining the tests? Turns out, we need to do that too.

The Importance of Maintaining Your Automated Tests
Maintaining automated tests will make them easier to modify and run smoothly, now and long-term. These simple steps keep a test suite in optimal condition.

Here's the real reason why it's important:

"To make matters worse, [testing] problems slowly compound until it eventually reaches a point where no one on the team wants to deal with them. Trust in the test suite diminishes, the tests become less and less valuable, and they become a burden on the team."

Code that no one trusts is worse than just having no code at all, and test code is still code. Keep your tests up to date!

Getting Prop Names from Lambda Expressions

Here's something I missed while I was out: a cool tip post from Khalid Abuhakmeh about how to get a property name from a lambda expression.

Get A Property Name From a .NET Lambda Expression
Learn how to use .NET lambda expressions to retrieve a PropertyInfo and property name.

It comes down to this code sample, pulled directly from that post:

using System;
using System.Linq.Expressions;
using System.Reflection;

public static class Lambdas
{
    public static PropertyInfo GetPropertyInfo<TType, TReturn>(
        this Expression<Func<TType, TReturn>> property)
    {
        LambdaExpression lambda = property;
        var memberExpression = lambda.Body is UnaryExpression expression
            ? (MemberExpression) expression.Operand
            : (MemberExpression) lambda.Body;

        return (PropertyInfo) memberExpression.Member;
    }

    public static string GetPropertyName<TType, TReturn>(
        this Expression<Func<TType, TReturn>> property
    ) => property.GetPropertyInfo().Name;
}

You can use these static extension methods to pull names of properties, including those in record classes:

using System;
using System.Linq.Expressions;
using Xunit;

public class LambdaTests
{
    [Fact]
    public void Can_get_name_of_Id()
    {
        Expression<Func<First,string>> expression = 
            f => f.Id;
        
        Assert.Equal(
            nameof(First.Id), 
            expression.GetPropertyName());
    }

    public record First(string Id);
    
}

Check out the post for more info!

This article is for paying subscribers only

Sign up now and upgrade your account to read the article and get access to the full library of articles for paying subscribers only.

Sign up now Already have an account? Sign in