SOLID / DRY principles

In software development, Object-Oriented Design plays a crucial role when it comes to writing flexible, scalable, maintainable, and reusable code. There are so many benefits of using OOD but every developer should also have the knowledge of the SOLID principle for good object-oriented design in programming. The SOLID principles are designed to help developers design robust, maintainable applications. The five SOLID principles are: Single-responsibility principle - Function and classes should have one job. Open–closed principle - Classes and functions should be open for  extension but not for modification. Liskov substitution principle - Child class can replace parent class Interface segregation principle - This principle is the first principle that applies to Interfaces instead of classes in SOLID and it is similar to the single responsibility principle. It states that “do not force any client to implement an interface which is irrelevant to them“. Here your main goal is ...

What is memoization

Memoization is a technique used in JavaScript (and other programming languages) to optimize the performance of functions by caching the results of expensive function calls. It involves storing the computed values of function calls and returning the cached result when the same inputs are provided again.


The idea behind memoization is that if a function is called with the same arguments multiple times, instead of recomputing the result every time, the function can return the previously computed result from a cache. This can significantly improve the execution time, especially for functions that involve complex or time-consuming computations.


Here's an example of a memoized function in JavaScript:



function memoizedFunction(n) {

  if (memoizedFunction.cache[n] !== undefined) {

    console.log("Fetching from cache");

    return memoizedFunction.cache[n];

  } else {

    console.log("Computing result");

    const result = n * 2;

    memoizedFunction.cache[n] = result;

    return result;

  }

}


memoizedFunction.cache = {};


console.log(memoizedFunction(5)); // Output: Computing result, 10

console.log(memoizedFunction(5)); // Output: Fetching from cache, 10

console.log(memoizedFunction(10)); // Output: Computing result, 20

console.log(memoizedFunction(10)); // Output: Fetching from cache, 20



In the example, the `memoizedFunction` takes a parameter `n` and computes the result by doubling it. The cache object is used to store previously computed results. On subsequent function calls with the same input, the function checks if the result is available in the cache. If found, it returns the cached result instead of recomputing it.


By memoizing the function, the expensive computation is performed only once for each unique input value, and subsequent calls with the same input retrieve the cached result. This can greatly improve performance, especially when dealing with recursive or repetitive computations.


It's worth noting that memoization is most effective for functions that are pure (i.e., their output is solely determined by their inputs and has no side effects). Functions that rely on external state or have side effects may not be suitable for memoization.


In JavaScript, there are also libraries and utilities available, such as Lodash's `memoize` function, that provide ready-made memoization implementations to simplify the process.

Comments

Popular posts from this blog

Difference between localStorage and sessionStorage

Scope in javascript