Skip to main content

Refactor a Function Without Lying

Retrieval Prompts​

  1. What does it mean for a function to do one thing?
  2. Why are boolean flags and long parameter lists often warning signs?
  3. What makes a side effect misleading rather than acceptable?
  4. Why does a function name need to reveal important side effects?

Refactor Checklist​

Before changing code, answer:

  • What behavior must stay identical?
  • Which lines are validation, which are core logic, and which are I/O?
  • Does the function name match what the function really changes?
  • What is the smallest safe extraction?

Common Mistake Check​

Do not split a function into three helpers that still depend on hidden shared state.

Do not rename a function to sound pure if it still mutates data.

Do not add new error-handling abstractions in this orientation exercise; record that question for Semester 3.

Mini Application​

Take one function with at least one of these smells:

  • more than 20 lines
  • four or more parameters
  • a boolean flag
  • visible side effects not revealed by the name
  • mixed validation and core logic

Refactor it in at least three small steps. After each step, write one sentence saying what became more honest.

Evidence Check​

You are done only when:

  • the final function names expose the main steps
  • the side effects are explicit
  • you can describe each refactor step independently