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 ...

Array slice method VS Array splice method in javascript

The `slice` and `splice` methods in JavaScript arrays are used to extract or modify portions of an array, but they have different behaviors and purposes.

1. `slice`:
   - The `slice` method returns a new array containing a shallow copy of a portion of the original array.
   - It takes two optional parameters: `start` and `end`, which specify the index range of the elements to be extracted (excluding the element at the `end` index).
   - The original array is not modified.
   - Example:


   const originalArray = [1, 2, 3, 4, 5];

   const newArray = originalArray.slice(1, 4);
   console.log(newArray); // Output: [2, 3, 4]

   console.log(originalArray); // Output: [1, 2, 3, 4, 5]


   In this example, `slice(1, 4)` extracts elements with indices 1, 2, and 3 (not including index 4) from the `originalArray` and returns a new array `[2, 3, 4]`.

2. `splice`:
   - The `splice` method is used to modify an array by adding, removing, or replacing elements.
   - It takes three or more parameters: `start`, `deleteCount`, and optional `item1, item2, ...`, where `start` is the index at which to start modifying the array, `deleteCount` specifies the number of elements to remove, and `item1, item2, ...` are the elements to be added.
   - It returns an array containing the removed elements.
   - The original array is modified.
   - Example:


   const originalArray = [1, 2, 3, 4, 5];

   const removedElements = originalArray.splice(1, 3, "a", "b", "c");
   console.log(originalArray); // Output: [1, "a", "b", "c", 5]
   console.log(removedElements); // Output: [2, 3, 4]
  

   In this example, `splice(1, 3, "a", "b", "c")` removes three elements starting from index 1 in the `originalArray` and inserts `"a"`, `"b"`, and `"c"` in their place. The modified array becomes `[1, "a", "b", "c", 5]`, and the removed elements `[2, 3, 4]` are returned.

The key differences between `slice` and `splice` are that `slice` returns a new array without modifying the original array, while `splice` modifies the original array and returns an array of the removed elements. Additionally, `slice` extracts a portion of the array based on the provided index range, while `splice` allows for adding, removing, or replacing elements at specific positions within the array.

Comments

Popular posts from this blog

Difference between localStorage and sessionStorage

Scope in javascript

What is memoization