Code Conciseness: Why Small Functions Make a Big Difference
Originally published on Medium: https://medium.com/@ezeanyimhenry/code-conciseness-why-small-functions-make-a-big-difference-78bf51357e32 In coding, less is often more. Small functions are easier t...

Source: DEV Community
Originally published on Medium: https://medium.com/@ezeanyimhenry/code-conciseness-why-small-functions-make-a-big-difference-78bf51357e32 In coding, less is often more. Small functions are easier to read, easier to debug, easier to reuse, and much easier to live with over time. The Trouble with Long Functions Long functions tend to hide intent. By the time you reach the end, you’ve forgotten what happened at the beginning. Short functions help because they make code: easier to understand easier to test and debug easier to reuse in different parts of a codebase A Simple Example Imagine a shopping cart where you want to calculate the total price of all items. Longer approach function calculateTotalPrice($cart) { $total = 0; foreach ($cart as $item) { $subtotal = $item['price'] * $item['quantity']; $total = $total + $subtotal; } return $total; } Concise approach function calculateTotalPrice($cart) { return array_sum(array_map(fn($item) => $item['price'] * $item['quantity'], $cart)); }