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.

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)}">
  ...
</div>

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