Distributed Systems 💻, getters & setters 🛠️, constructors 👷, | | ⚔️ ??

By Prajwal Haniya

Techletter #72 | May 11, 2024

What are distributed systems?

A distributed system is nothing but a system where multiple computers collaborate to perform a task or operation where the computers are spread across the network.

Distributed systems are important as high performance, highly scalable and fault tolerance systems are built using them.

A complete lecture series that I am watching at present is on distributed systems. You can watch it here.

What are getters and setters in TypeScript?

Getters and setters in TypeScript are used to access and modify the properties of a class in a controlled manner.

Getters are methods that allow you to retrieve the value of a private member, while setters enable you to set the value of a private member.

Getters are defined using the get keyword, and setters are defined using the set keyword.

class TemperatureConverter {
    private _celsius: number = 0;

    // Getter method to retrieve temperature in Celsius
    get celsius(): number {
        return this._celsius;
    }

    // Setter method to set temperature in Celsius
    set celsius(value: number) {
        if (value >= -273.15) {
            this._celsius = value;
        } else {
            console.error("Invalid temperature. Must be greater than or equal to -273.15°C.");
        }
    }
}

const temp = new TemperatureConverter();
temp.celsius = 12;
console.log(temp.celsius);

Access Modifiers for constructors in TypeScript

There are three types of access modifiers for constructors

A public constructor is the default behavior in TypeScript. It means that the constructor can be accessed and called from anywhere in the class and outside the class.

class Person {
    constructor() {}
}

A private constructor is used to restrict access to the constructor and its parameters. This means that the constructor can only be accessed and called within the class itself, and not from outside the class or its derived classes. This is useful for encapsulating implementation details and preventing accidental or unintended modification of class properties.

class SingletonExample {
  private static instance: SingletonExample;

  private constructor(private value: number) {}

  public static create(value: number): SingletonExample {
    if (!SingletonExample.instance) {
      SingletonExample.instance = new SingletonExample(value);
    }
    return SingletonExample.instance;
  }

  public getValue(): number {
    return this.value;
  }
}

// Attempting to create instances directly will result in an error
// const instance1 = new SingletonExample(10); // Error: Constructor of class 'SingletonExample' is private

// Creating instances using the factory method
const instance1 = SingletonExample.create(10);
const instance2 = SingletonExample.create(20);

console.log(instance1.getValue()); // Output: 10
console.log(instance2.getValue()); // Output: 10 (same instance as instance1)

A protected constructor is similar to a private constructor but allows access within the class and its derived classes. This is useful for implementing inheritance, where derived classes need to access and modify the properties of the base class.

class Animal {
  protected constructor(protected name: string) {}

  public introduce(): void {
    console.log(`Hello, I am ${this.name}.`);
  }
}

class Dog extends Animal {
  private breed: string;

  constructor(name: string, breed: string) {
    super(name);
    this.breed = breed;
  }

  public displayInfo(): void {
    console.log(`Name: ${this.name}, Breed: ${this.breed}`);
  }
}

// Attempting to create an instance of Animal directly will result in an error
// const animal = new Animal("Spot"); // Error: Constructor of class 'Animal' is protected

const dog = new Dog("Buddy", "Labrador");
dog.introduce(); // Output: Hello, I am Buddy.
dog.displayInfo(); // Output: Name: Buddy, Breed: Labrador

What is the difference between ?? (null coalescing operator) and || (logical OR Operator)?

The difference between the ?? and || operators in JavaScript lies in their behavior when dealing with falsy values and nullish values.

The || operator returns the first truthy value it encounters when evaluating a set of expressions. If all expressions are falsy, it returns the last falsy value.

The ?? operator returns the first defined value (not null or undefined) it encounters when evaluating a set of expressions. If all expressions are nullish, it returns the last expression.

When to use ||

When to use ??