Day 2 of 5
⏱ ~60 minutes
Clean Code in 5 Days — Day 2

Functions & Single Responsibility

Small functions, pure functions, avoiding side effects.

What You'll Cover Today

Day 2 of Clean Code in 5 Days focuses on functions & single responsibility. Small functions, pure functions, avoiding side effects. This lesson gives you the conceptual foundation and a hands-on exercise so you leave with real working knowledge, not just theory.

ℹ️
Topics today: SRP, pure functions, side effects. Each section has code you can copy and run immediately.

SRP

Understanding srp is foundational for everything in this course. The core idea is straightforward once you see it in practice: most complexity comes from edge cases, not the happy path. Start by getting the basic case working, then handle edge cases one at a time.

Example — SRP
// Functions & Single Responsibility — working example
// Replace these values with your actual data

const example = {
  topic: 'SRP',
  day: 2,
  course: 'Clean Code in 5 Days'
};

// This is where your implementation goes
function implement(config) {
  // 1. Validate inputs
  if (!config.topic) throw new Error('Topic required');
  
  // 2. Core logic
  const result = process(config);
  
  // 3. Return structured output
  return result;
}

console.log('Ready to implement SRP');

pure functions

Once you have the basics, pure functions becomes the practical application. The pattern you'll use most often is: configure once, reuse everywhere. Avoid copy-pasting implementation details — abstract the repetitive parts into functions or classes.

💡
Pro tip: When working with pure functions, always read the official documentation for the exact API version you're using. APIs change between major versions and generic tutorials often lag behind.

Common Mistakes on Day 2

📝 Day 2 Exercise
Functions & Single Responsibility — Hands-On
  1. Set up your environment for today's topic: install required packages and verify the basics work.
  2. Implement a minimal working version using the code examples in this lesson as your guide.
  3. Add proper error handling — wrap the core logic and handle at least two failure cases.
  4. Test your implementation with both valid and invalid inputs.
  5. Review your code: is there anything you'd name differently? Any function doing more than one thing?

Day 2 Summary

  • SRP is the foundation — understand it before moving on.
  • pure functions is how you apply it in real projects.
  • Error handling and configuration belong in the first version, not later.
  • Read error messages carefully — they usually tell you exactly what's wrong.
Challenge

Extend today's exercise by adding one feature that wasn't in the instructions. Document what you built in a comment at the top of the file. This habit of going one step further is what separates developers who grow fast from those who stay stuck.

Finished this lesson?