The Cult of DRY

As developers, most of us are inducted into the cult of common code and Don’t Repeat Yourself (DRY) early on in our software engineering educations and careers. This was certainly the case for me. The idea sticks with us because it just makes so much sense: why write more code when you could write less? Why slog through making changes in dozens of places when you could have updated a single bit of centralized logic? Why re-invent the wheel when you can use a battle-tested implementation that already exists?

Over time, however, I’ve come to see cracks in this philosophy, or at least the absolutist version of it. DRY is a powerful software engineering principle, but it is not the only software engineering principle. As codebases and teams get larger, a religious adherence to DRY can be crippling to maintainability and development time.

Continue reading The Cult of DRY

Random numbers in C# .NET: a primer

On the surface, .NET’s Random class seems comparable to the random number APIs in other frameworks. However, it has a number of quirks that can make it tricky to use correctly. With randomness, bugs can manifest quite subtley: rather than an exception you might just end up slightly biased load balancing or a playlist shuffle that picks the same song first more often than not. Therefore, it’s worth taking the time to make sure you know what you’re doing! This post covers the basic usage patterns for the random class, as well as the gotchas and how the MedallionRandom NuGet Package can help fill in the gaps.

Continue reading Random numbers in C# .NET: a primer

Generic Tree and Linked List Traversal in C#

When you think of traversing a tree, what comes to mind? Recursion? Looping with a stack? While both of these methods work perfectly well, neither has the elegance, convenience, composability, and laziness of IEnumerable-based traversal using LINQ. Luckily, it isn’t hard to “factor out” the mechanics of traversing trees and tree-like data structures such that we can cleanly process such objects with LINQ without writing lots of boilerplate code first. In this post we’ll look at a couple functions in the MedallionCollections NuGet package that do just that.

Continue reading Generic Tree and Linked List Traversal in C#

Inline NuGet packages: a solution to the problem of utility libraries

Developers love utility functions, and every developer seems to have his or her favorite set. In my various projects, I find myself using the same utilities over and over again, copying them from project to project. This gets frustrating and wastes time, especially when I find a bug or performance issue in a utility and want to get it updated everywhere. The obvious solution is to wrap my favorite utilities up in their own NuGet package, but this has several disadvantages which I’ll describe further on. To address this issue, I’ve created a tool which allows for the creation of “inline” NuGet packages which are, in my mind, a particularly appropriate way to distribute utilities.

Continue reading Inline NuGet packages: a solution to the problem of utility libraries

Engineering a Collection Equality Function

I’ve mentioned before that collection equality is a relatively common and useful operation that has no built-in implementation in the .NET framework:

bool CollectionEquals<t>(
    this IEnumerable</t><t> @this, 
    IEnumerable</t><t> that, 
    IEqualityComparer</t><t> comparer = null);
</t>

Here I’m differentiating collection equality from sequence or set equality: I want two collections to be considered equal if and only if they contain exact same elements, respecting duplicates but disregarding order. While I’ve implemented this functionality in the past, for a new utility library I’m creating I wanted to see how much I could tune the implementation.

Continue reading Engineering a Collection Equality Function

Distributed Locking with .NET and SQLServer

Locks are probably the most prolific synchronization construct in all of programming. The premise is simple: carve out a region of code that only one thread can enter at a time. Most programming languages provide support for locking mechanisms in their standard libraries if not (as in C#) in the languages themselves.

Unfortunately, these default tools often start to break down when we start to think about distributed locking. That is, when we don’t want to limit execution just to one thread in the current process but instead to one thread in an entire system or even in a cluster of systems.

Continue reading Distributed Locking with .NET and SQLServer