ISO 6346 – Shipping Container Codes

I often spend time researching and implementing algorithms in multiple languages as a way to keep my programming skillset sharp, learn a new language and its syntactical sugar, and to have the algorithms at my disposal when and if I need them. ISO 6346 is an international standard that describes the identification of a shipping container. The standard is maintained by the BIC (International Container Bureau) and covers the serial number, owner, country code, and size of any given shipping container.

Using AWS CDK with dotenv -r and TypeScript

Note, I prefer the node --require option of loading .env variables over importing or requiring in application code. TLDR; install dotenv as a development dependency in the cdk.json in the root of the project directory add the following in bold : { "app": "npx ts-node -r dotenv/config --prefer-ts-exts bin/<stack-name>.ts" } When developing via AWS CDK it is easy to set environment …

Preload dotenv, don’t require it

Oh my dotenv! If you work with NodeJS at some point you will use and implement dotenv. It loads environment variables from a .env file (configurable name btw), into process.env, following the 12 Factor App methodology of separating environment from code. Pure awesomeness, and makes development configuration loading a breeze mimicking your higher environments.

Avoid using ts-node when performance testing

Often times when performance testing, or comparing resource utilization (ex NodeJS vs Java), I have seen developers load testing, performance testing, and even one-off testing, with ts-node. ts-node is an awesome piece of kit, but it is for rapid prototyping, watching, and development only, it is not a production runtime.

The awesome Readonly<T> Utility Type

Lets all be honest, side effects are terrible. They are especially terrible when working with a runtime that doesn't implicitly discern between mutable and immutable pre transpilation or compilation (like JavaScript). I have often found functions during code review where an Array or Object is manipulated in the subroutine returning the input Array or Object. Ideally, functions should remain pure.

Enum Flags … are … awesome!

An often overlooked feature of most if not all languages is the concept of Enum Flags. In a nutshell, Enum Flags are an efficient way of storing and representing a collection of boolean values with one value 😎. This applies to all SQL languages, C#, Java, Python, Rust, GoLang, the list goes on, and illustrated here in TypeScript.

Literal Types / Type Guards

When using TypeScript, we often (and hopefully as a manner of practice, always) declare the type of a variable or property. TypeScript allows the possibility of a variable or property to be of one or more types. This means that a variable or property can be either a number, string, or custom type by declaring it like so...

Use for..of in lieu of traditional incremental for loops when appropriate

For loops are for loops, sure. There is absolutely nothing wrong with the traditional incremental iterator approach. However, this recommendation is about readability, maintenance, decreasing code smells, as well as best practice when using loops in TypeScript (and in this case ES6+ as well). Often during code review, one will find references to an item via its parent array and index numerous times (think myArray[i].firstName and guess how lastName is accessed? 😞). It is a best practice in most if not all languages to create a variable out of this value accessed by index once.

JS Array Functions Instead of Lodash

Lodash is an amazing library and is often used to minimize the hassle of working with arrays, numbers, objects, strings, etc. However, it is often seen that one of its primary uses is array manipulation, and accessing elements within. TypeScript (and ES6) have the majority this functionality built in!