API Names and Static Polymorphism

.

While static polymorphism is often discussed in the context of C++ (in particular, related to CRTP and method overloading), we can generalize the concept to help us choosing the most optimal function and property names of a public interface. This also applies to JavaScript API, of which some examples and illustrations are given in this… Read more »

JavaScript String: substring, substr, slice

.

Extracting a portion of a string is a fairly well understood practice. With JavaScript, there are three different built-in functions which can perform that operation. Because of this, often it is very confusing for beginners as to which function should be used. Even worse, sometimes it is easy to fall into the trap and choose… Read more »

JavaScript Array: slice vs splice

.

In JavaScript, mistaking slice for splice (or vice versa) is a common mistake among rookies and even experts. These two functions, although they have similar names, are doing two completely different things. In practice, such a confusion can be avoided by choosing an API that telegraphs the const-correctness of the function. Array’s slice (ECMAScript 5.1… Read more »

Detecting Nested Ternary Conditionals

.

Nested ternary conditional operators can be useful. In some cases, e.g. to promote better code readability, you may want to outlaw that practice. Armed with a code parser, a simplistic code analysis allows such a pattern detection. Once somebody understands the power of the ternary operation, it is very tempted to harness it in multiple… Read more »

Ternary Conditional and Boolean Values

.

Many programming languages support the ternary conditional operator. It is very useful to shorten the code since something like: if (fast) speed = 70; else speed = 25; can be rewritten to look like the following (C-style syntax). In such a form, it is also concise, readable, and unambiguous. speed = fast ? 70 :… Read more »

Detecting Boolean Traps with Esprima

.

A bad API can lead to ambiguities and reduced readability. One very common thing I still encounter in various JavaScript frameworks is the so-called Boolean trap, i.e. the unwise use of Boolean argument(s) in a function call. Reviewing or reading code which gets trapped there is not fun. The earlier we can catch such as… Read more »