Posts

Showing posts from November, 2024

SOLID principles for software development

SOLID principles  are five design guidelines for writing clean, maintainable, and scalable object-oriented software. The acronym stands for: S - Single Responsibility Principle (SRP) : A class should have one and only one reason to change. O - Open/Closed Principle (OCP) : Software entities should be open for extension but closed for modification. L - Liskov Substitution Principle (LSP) : Subtypes must be substitutable for their base types. I - Interface Segregation Principle (ISP) : No client should be forced to depend on methods it does not use. D - Dependency Inversion Principle (DIP) : Depend on abstractions, not on concrete implementations. 1. Single Responsibility Principle (SRP) Definition : A class should only have one responsibility or reason to change. Why? Simplifies maintenance and avoids unintended side effects when modifying code. Example : Bad : A class managing both user authentication and database operations. Good : Separate classes...

Two approaches of parsing a JSON response

Both approaches achieve the same goal of parsing a JSON response, but they do so in slightly different ways: 1. Using `response.json()`:    ```javascript    const obj = await response.json();    ```    This method is more straightforward and concise. It directly parses the response body as JSON and returns a JavaScript object. It's the preferred method when you know the response is in JSON format. 2. Using `response.text()` and `JSON.parse()`:    ```javascript    const objText = await response.text();    const obj= JSON.parse(objText );    ```    This method first reads the response body as a text string and then parses that string into a JavaScript object using `JSON.parse()`. This approach can be useful if you need to manipulate the raw text before parsing it as JSON or if you're dealing with a response that might not always be JSON. In most cases, using `response.json()` is simpler and more...