Keep Your Code Clean by Naming Things Meaningfully

Naming things (= variables, properties, functions, methods, classes) correctly and in an understandable way is an extremely important part of writing clean code. Be Descriptive Names have one simple purpose: They should describe what’s stored in a variable or property or what a function or method does. Or what kind of object will be created when instantiating a class. If you keep that in mind, coming up with good names should actually be straightforward - though coming up with the best name for a given variable/ property/ function/ … will of course still require some practice and often multiple iterations....

December 28, 2022 · 3 min · Daman Arora

A Word About Code Coverage in Testing

An important aspect of testing is to achieve good code coverage. This means, that you want to write tests for the majority of your code (both code files and line of code). There are tools that help you measure your code coverage - Testing library, Vitest comes with a built-in functionality: https://vitest.dev/guide/features.html#coverage It is worth noting though, that the goal is not necessarily 100% coverage. There always can be some code that doesn’t need any tests (e....

December 28, 2022 · 1 min · Daman Arora

Use Unary Operator to Convert Non Numbers to Numbers

The unary plus (+) operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn’t already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null....

December 28, 2022 · 1 min · Daman Arora

Always Follow AAA Pattern When Writing Tests

Following is a very simple unit test which verifies if the add() function works correctly. While the test works, but it does not follow the AAA(Arrange, Act, Assert) pattern. import {it, expect} from 'vitest'; import { add } from './math'; it('should summarize all number values in an array', ()=>{ const result = add([1,2,3]) expect(result).toBe(6) }); Question: What is AAA (Arrange, Act, Assert) pattern in testing? AAA pattern allows you to write unit tests which are more readable and maintainable in long term....

December 27, 2022 · 1 min · Daman Arora

Modules and Classes in ES6

Modules ES6 has added Modules to JavaScript. This means, that you may split up your code over multiple files, which of course is a good practice. This is common in ES6 already, however you always require a module loader for that. To split up your code, you basically export variables, functions, objects, … in one file and import it in another: // export.js export let myExportedVar = 42; // import.js import { myExportedVar } from '....

December 26, 2022 · 1 min · Daman Arora