Wednesday, February 1, 2017

When do I want to break up the falsey five?

Almost never. The convenience of checking to see if something is falsey is a convenience for a reason, you know? The one exception might be to see if a numeric property is set yet or not. In those cases you would want a distinction between a zero versus null and/or undefined. Do something like so:

if (whatever || whatever === 0) {

 
 

Something to avoid is to write a separate pipe (in an Angular 2 app for example) that tries to return true or false based on if something is undefined or null. The one line comparison against zero is the way to keep it simple. While I'm thinking of pipes, don’t use a pipe directly in TypeScript. If you need a repurposable blob of functionality that can work in both the front face Angular 2 markup and the TypeScript files you should write a service for it and then have a shim of a pipe for wrapping the service that exclusively is used in the markup.

No comments:

Post a Comment