{"id":29,"date":"2025-01-16T18:03:12","date_gmt":"2025-01-16T18:03:12","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/devindaniels\/?p=29"},"modified":"2025-01-16T18:03:12","modified_gmt":"2025-01-16T18:03:12","slug":"functions-should-do-one-thing","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/devindaniels\/2025\/01\/16\/functions-should-do-one-thing\/","title":{"rendered":"Functions Should Do One Thing"},"content":{"rendered":"\n<p><br>When writing functions, it\u2019s tempting to cram multiple operations into one large function. Sometimes deadlines are tight, fatigue sets in, or laziness creeps up. However, this approach makes your code harder to read, debug, and maintain. A good function should do just one thing\u2014and do it well. If a function performs several tasks, it should be broken into smaller functions, each focused on a single responsibility.<\/p>\n\n\n\n<p>Here\u2019s a JavaScript example inspired by <em>Clean Code Handbook (page 302)<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function pay(employees) {\n  employees.forEach((employee) =&gt; {\n    if (employee.isPayday) {\n      const pay = employee.salary * employee.hoursWorked;\n      employee.bankAccount.deposit(pay);\n      console.log(`Paid ${pay} to ${employee.name}`);\n    }\n  });\n}<\/code><\/pre>\n\n\n\n<p>While the function itself is only 9 lines long,\u00a0it violates the principle of a single responsibility. When a function is written like this, it makes it more difficult to read, refactor, and test.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function pay(employees) {\n  employees.forEach((employee) =&gt; processPayment(employee));\n}\n\nfunction processPayment(employee) {\n  if (isPayday(employee)) {\n    const pay = calculatePay(employee);\n    deliverPay(employee, pay);\n    logPayment(employee, pay);\n  }\n}\n\nfunction isPayday(employee) {\n  return employee.isPayday;\n}\n\nfunction calculatePay(employee) {\n  return employee.salary * employee.hoursWorked;\n}\n\nfunction deliverPay(employee, amount) {\n  employee.bankAccount.deposit(amount);\n}\n\nfunction logPayment(employee, amount) {\n  console.log(`Paid ${amount} to ${employee.name}`);\n}<\/code><\/pre>\n\n\n\n<p><strong>Benefits of Smaller Functions<\/strong><\/p>\n\n\n\n<p>Breaking down functions into single-responsibility components provides several advantages:<\/p>\n\n\n\n<p><strong>1. Improved Readability<\/strong><\/p>\n\n\n\n<p>Each function is short, descriptive, and easy to understand. You don\u2019t need to wade through the logic to figure out what <code>calculatePay<\/code> or <code>deliverPay<\/code> does. Their purpose is clear from their names.<\/p>\n\n\n\n<p><strong>2. Easier Maintenance<\/strong><\/p>\n\n\n\n<p>When a requirement changes\u2014say, you need to update how payments are logged\u2014you can modify just the <code>logPayment<\/code> function without affecting the rest of the system. This reduces the risk of introducing bugs elsewhere.<\/p>\n\n\n\n<p><strong>3. Simpler Testing<\/strong><\/p>\n\n\n\n<p>Testing smaller functions is much easier than testing a monolithic one. You can write unit tests for <code>isPayday<\/code>, <code>calculatePay<\/code>, or <code>logPayment<\/code> independently, ensuring that each part of your application works correctly.<\/p>\n\n\n\n<p><strong>4. Separation of Concerns<\/strong><\/p>\n\n\n\n<p>By dividing responsibilities, you make your code modular. This modularity allows you to isolate logic, leading to better architecture and long-term scalability.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Clean, maintainable code is not about adding more lines; it\u2019s about structuring those lines intelligently. Writing functions that do one thing is a fundamental principle of clean code, and it pays off in readability, maintainability, and testability.<\/p>\n\n\n\n<p>Next time you\u2019re tempted to write an all-in-one function, pause and ask yourself: <em>Can this be broken into smaller functions?<\/em> The answer will likely lead to cleaner, more efficient code.<\/p>\n\n\n\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When writing functions, it\u2019s tempting to cram multiple operations into one large function. Sometimes deadlines are tight, fatigue sets in, or laziness creeps up. However, this approach makes your code harder to read, debug, and maintain. A good function should do just one thing\u2014and do it well. If a function performs several tasks, it should &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/devindaniels\/2025\/01\/16\/functions-should-do-one-thing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Functions Should Do One Thing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14531,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/users\/14531"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/posts\/29\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/devindaniels\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}