Script Valley
Writing Clean Code: Naming, Functions & Structure
Writing Functions That Do One Thing/Assessment

Practice & Assessment

Test your understanding of Writing Functions That Do One Thing

Multiple Choice Questions

6
1

A function named 'saveAndNotify(user)' saves to the database and sends an email. What clean code principle does this violate?

2

What is a flag argument and why is it considered a code smell?

3

Which of the following is a pure function?

4

When is the best time to apply 'Extract Method' refactoring?

5

A function 'createReport(title, type, date, format, user, includeCharts)' has six parameters. What is the best fix?

6

What does the 'indentation test' tell you about a function?

Coding Challenges

1
1

Refactor a Monolithic Order Processing Function

You are given a single JavaScript function 'processOrder(order)' that is 80 lines long and performs: input validation, stock checking, price calculation with discounts, order record insertion, inventory update, confirmation email formatting, and logging. Input: the provided monolith.js file. Output: refactored.js where the original function has been split into at minimum 6 clearly named, single-purpose functions, each under 15 lines. The final orchestrating function should read like a high-level summary of the process. All original logic must be preserved โ€” only structure changes. Estimated time: 25 minutes.

Medium

Mini Project

1

Clean Functions: Authentication Module Rewrite

You are given a working but messy authentication module (login, register, logout, password reset) implemented as four monolithic functions averaging 60 lines each. Each function mixes validation, database calls, token generation, email sending, and logging. Your task: rewrite the module so that every function has a single responsibility, no function exceeds 15 lines, parameter objects replace any function with more than 2 parameters, all side effects are in explicitly named functions, and the four main functions read as clean orchestrators. Write a short README explaining every extraction decision you made. The module must pass all provided unit tests after your rewrite.

Medium
Practice & Assessment โ€” Writing Functions That Do One Thing โ€” Writing Clean Code: Naming, Functions & Structure โ€” Script Valley โ€” Script Valley