Recurse.se Yada yada on Software Development

5Jul/170

Accessing the Aurelia viewModel from the browser

If you want to accessing the Aurelia viewModel from the browser console, note that there's a difference between the element hosting the Aurelia app itself, and an element hosting an Aurelia component. If the app is hosted on the body element (<body aurelia-app="main">) you can use;

document.querySelector('body').aurelia.container.viewModel or
document.querySelector('body').aurelia.root.viewModel

and in the component case, you can find the viewModel via

document.querySelector('my-custom-element').au.controller.viewModel

Filed under: Aurelia, Frontend No Comments
4Jul/170

Empty if null – Preventing null infections

In OO-style development, the value null causes endless issues. In FP-style development null is a much smaller problem, because using null isn't ideomatic style. In FP, instead of using null to indicate absence, lists tend to be empty, and optional values tend to use Option (a.k.a Maybe) types.

In one of our C# codebases, which is predominantly OO, using FP-style list processing often runs into the null problem.

Code like:

var currencies = contract.sections.Select(s => s.currency).Distinct();

raises exceptions if 'sections' is null, and constantly guaranteeing that all properties we use list operations on isn't null is a pain and breaks the chain of list processing.

We could have used the null conditional operator (?.) and written:

var currencies = contract.sections?.Select(s => s.currency).Distinct();

but this just propagates the the null value, spreading the null-infection downstream! Now currencies is either a list of currencies, or null.

Enter one of the most useful extension methods I've come up with, EmptyIfNull;

public static class EnumerableExtensions
{
  public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> items)
  {
    return items ?? Enumerable.Empty<T>();
  }
}

Now we can write:

var currencies = contract.sections.EmptyIfNull().Select(s => s.currency).Distinct();

which won't raise an exception even if 'sections' is null, allowing the code to flow more naturally, and we are also guaranteed that currencies is always a list (though maybe empty) but never null.