The Power of Parameter Names

C# 4 introduced named and optional parameters: the ability to set default values for arguments and explicitly label arguments by name instead of just position. Despite being a relatively minor features, named and optional parameters actually do quite a lot to make coding simpler to write, read, and consume. So much so, that I find myself sorely missing this feature when working in languages without it (e. g. JavaScript).

Continue reading The Power of Parameter Names

Better validation with yield and async/await: the wrapper pattern

Recently, I’ve been writing a lot of code which gets packaged up into libraries to be consumed by other developers. One of the key aspects of writing this sort of code effectively is to be stringent about argument validation. The earlier your code detects invalid arguments, the easier it is for consumers of your APIs to diagnose errors. Argument checking is fairly straight-forward, but I recently came upon an interesting subtlety when working with the C# yield keyword.

Continue reading Better validation with yield and async/await: the wrapper pattern

Promises and the danger of angular.noop

Recently I started working with AngularJS. One of things I enjoy most about AngularJS are promises. Promises are a design pattern / module useful for asynchronous tasks. Normally, I rave about how awesome promises are, but instead of raving I wanted to talk about something that tripped me up when I first started using promises.

The following promises are not equivalent. And I’m going to explain why.

var deferred = $q.defer();
deferred.promise.then(mySuccessCallback, angular.noop, myNotifyCallback);
deferred.promise.then(mySuccessCallback, null, myNotifyCallback);

Continue reading Promises and the danger of angular.noop

Creating a dynamic OData grid with MedallionOData and JQuery DataTables

LINQ providers such as Entity Framework (and therefore any OData services built on top of them) are heavily reliant on server-side static typing to define the model. This has a number of benefits, such as allowing for automatic strongly-typed endpoint code generation, more robust parsing of OData queries, and added security. However, many practical applications don’t fit nicely into the static query model. Luckily, the MedallionOData library provides an easy solution through it’s ability to create dynamic OData service endpoints. We’ll walk through the setup step-by-step in the context of a toy example.

Continue reading Creating a dynamic OData grid with MedallionOData and JQuery DataTables

Even more concise C# object initializers

C# object, array, and collection initializers provide a concise and convenient syntax for building objects in a single, JSON-like code statement. For example, many examples of constructing a Process object show something like the following:

var process = new Process { StartInfo = new ProcessStartInfo { FileName = "a.exe" } };

However, a less common form of object initialization can make such code even more terse.

Continue reading Even more concise C# object initializers

Working with processes in .NET

Scripting and shell languages are often built around the ability for one process to easily launch and work with the results of others. This is the primary mode of processing in bash, while ruby supports at least 5 built-in approaches with varying levels of flexibility and conciseness.

In .NET, this is kind of operation is typically done via the System.Diagnostics.Process API. The Process API is quite general and powerful, but it can be clunky and difficult to use correctly in the common use cases that are handled so well by the languages above. As a spoiler, I ended up wrapping much of this complexity into a new .NET library: MedallionShell. With MedallionShell, this kind of task is a one-liner:

var output = Command.Run(pathToExecutable, arg1, ...).Result.StandardOutput;

Continue reading Working with processes in .NET

Cached asset files

Recent I’ve been working on a side project Battle States. It’s a simple game modeled off of Mr. Scocus’ (my 6th grade Social Studies teacher) competition that helped us learn state/country names and capitals. I can name all 50 state capitals thanks to him! The backened is powered by django, but most of the logic is written in Javascript. I noticed while developing and in production that often my browser was too aggresively using cached asset files rather than pulling updated assets from my server. This problem was particularly concerning because so much of the logic is written in Javascript.

Continue reading Cached asset files