From bc4d86ba96d9a8d76e3a9ffe5823138214527495 Mon Sep 17 00:00:00 2001 From: mohan Date: Wed, 14 May 2025 08:25:47 +0530 Subject: [PATCH] Add examples for pure functions and destructuring defaults --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/README.md b/README.md index f1159781..b48537c0 100644 --- a/README.md +++ b/README.md @@ -1033,6 +1033,40 @@ function newRequestModule(url) { const req = newRequestModule; inventoryTracker("apples", req, "www.inventory-awesome.io"); ``` +**[⬆ back to top](#table-of-contents)** + +#### Pure Functions vs. Side Effects + +- A **pure function** only uses its input values and doesn’t change anything outside itself. +- A **side effect** is any change to external state (mutating arguments, globals, etc.). + +**Why?** +- Predictable: gives the same result every time for the same inputs. +- Easy to test: no hidden state to reset or mock. +- Safe to reuse and refactor without unintended consequences. + +**Bad:** +```js +let total = 0; + +function addToCart(cart, item) { + cart.push(item); // ← side effect: changing external cart + total += item.price; // ← side effect: changes external `total` + return cart.length; +} +``` +**Good:** +```js +function addToCart(cart, item) { + // returns a new cart array without touching the original + return [...cart, item]; +} + +function calculateTotal(cart) { + // pure computation: only reads input, never writes out + return cart.reduce((sum, item) => sum + item.price, 0); +} +``` **[⬆ back to top](#table-of-contents)** @@ -1136,6 +1170,35 @@ console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe delete employee.name; console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe ``` +**[⬆ back to top](#table-of-contents)** + +#### Destructuring Defaults & Nested Destructuring + +It lets you set default values for object properties right in the function signature and prevents errors if a nested object isn’t provided. + +**Why?** +- Eliminates repetitive `||` defaulting. +- You can see what values the function uses and their fallback values right in its definition. +- Prevents runtime errors when nested objects are omitted. + +**Bad:** +```js +function connectToDatabase(opts) { + const host = opts.host || 'localhost'; + const port = opts.port || 80; + const db = opts.db || { name: 'test' }; +} +``` + +**Good:** +```js +function connectToDatabase({ + host = 'localhost', + port = 80, + db: { name = 'test' } = {} +} = {}) { +} +``` **[⬆ back to top](#table-of-contents)**