Thoughts were had

All posts

Railway Oriented Programming in Javascript

A mental model for promise chains and error handling, adapted to javascript promises. This post is entirely based on the post and talk Railway Oriented Programming by Scott Wlaschin.

I am quite fascinated with our tendency to focus on the happy code path, which often leaves error handling as an afterthought. It always bothered me, and with the recent popularization of functional programming techniques I think I have finally found a mental model for error handling that I like. It’s the either monad!

As it turns out, promises in Javascript are quite analogous to the Either monad. If you have no idea what that means, I hope the Railway analogy will help you as it did me.

Most functions that are doing external calls like HTTP requests, file system operations or runtime validation will have at least two different outputs: a success case and an error case.

Read more

Posted in Programming FP, Promises 0 Comments

Abstractions are mental overhead

It’s incredible how quickly everything moves in our field. Nothing makes this more clear than looking through our own old code, or in this example old posts right here on the blog.

The quest for not repeating anything makes us do crazy hacks and abstractions, just to avoid having those very error prone background-color: red; “duplicated” between two files.

Most abstractions seem beneign at conception, but tend to mutate with time. The more abstractions it has, the more cracks will appear when you need to move or change an interface between classes/modules. When a feature needs adding, and falls into the void, who knows where it ends up? Sometimes math.random() might as well decide, since “anywhere” will seem perfect when deadlines loom.

Even when you perfectly understand the entire codebase, figuring out which classes to change can be hard. Now pass this code on to the next guy, who has none of the context. How many concepts will they have to learn in order to work in the framework and vocabulary that you have created without breaking it?

Avoid leading your colleagues deeper into the rabbit hole of random abstractions by keeping the APIs simple. Use simple or common datatypes where possible.

Avoid over engineering your APIs to please the OOP gods.

Don’t try to be clever with indirections

Tunnel vision comes in many forms. Trying to decouple your code is another example. In this example, a post from MSDN called Active events. Basically, this article describes a pattern to avoid having any explicit dependencies in your code, in the attempt to make it easier to replace parts of it with another DLL.

This hits a nerve with me, as implicit dependencies are at the core of many bugs I’ve seen lately. You might be able to replace the AuthenticationProvider from any file in the project now, but is this a fox in sheeps clothes? I think it is, since you have most certainly just confused your IDE, and possibly also your colleagues. If the code that runs when invoking dispatch("auth-please") can live anywhere, you might as well have just done address space randomization on you project files. You have decoupled the code, your IDE and any possibility of doing explorative discovery to learn how it works.

Working in a system of implicit dependencies can be extremely draining, since the effects of change are hidden behind layers of indirection and abstraction. You get zero help from standard tooling. The less your tools can help you, the more mental capacity is needed to juggle the parts and their relationships. Even more so, to start changing these relationships when requirements inevitably change.

Read more

Posted in Programming 0 Comments

Extending $q promises in Angular

Why another post about this? Most implementations seem to overlook the fact that the changes applied to delegate.defer only affect the first promise in the chain, since by design the defer function used internally in Angular cannot be modified.

Since we can affect the first returned promise we have our way in. Now to make sure we stay “in”.

Read more

Posted in Programming Angular, Promises 0 Comments

Form validation with rcSubmit and bootstrap

I have long been annoyed with doing form validations in Angular, it just never felt right. I’d manually link every input to a form with some convoluted ng-class conditions, not to mention track form state in controllers and polluting the scopes with loading indicators.

I just recently found an excellent writeup on using the new ngMessages module in Angular >=v1.3.0.beta.8 have your error messages prioritized and easily customized. This made me rethink and rewrite the way I use forms.

Which brought me back to rcSubmit and the source. With rcSubmit the state tracking and loading indicators are handled for us, but spamming my forms with

<div ng-class="{has-error: rc.form.needsAttention(form.field)}">

feels too repetitive.

If we make a few assumptions on form structure this can easily be avoided with a few directives.

Read more

Posted in Programming Angular, Validation 0 Comments

Global stylus variables with grunt-stylus

Stylus is a CSS preprocessor that enables the definition of various helpers in the form of mixins and variables for configuration.

You should already know the awesome nib stylus extensions. You might even have some helpers yourself, that you want to be accessible in all your stylus files. Perhaps even included by default, saving your the trouble of writing

@import 'nib';
@import '../my/helpers';

In all your files. Dont worry, grunt-stylus has you covered.

Read more

Posted in Programming Grunt 0 Comments