Declarative vs Imperative
We should always prefer writing declarative code when possible instead of imperative code. The declarative approach focuses on expressing the developer’s intent clearly rather than manually writing instructions on how the program should achieve that intent.
Examples
Section titled “Examples”Imperative code (Incorrect ❌)
Section titled “Imperative code (Incorrect ❌)”interface Item { isActive: boolean; value: string;}
function handleItemsImperatively(items: Item[]) { const results = []; for (let i = 0; i < items.length; i++) { if (items[i].active) { results.push(items[i].value); } } return results;}Declarative code (Correct ✅)
Section titled “Declarative code (Correct ✅)”type Item = { isActive: boolean; value: string;};
const handleItemsDeclaratively = (items: Item[]) => { return items.filter((item) => item.active).map((item) => item.value);};