Script Valley
Writing Clean Code: Naming, Functions & Structure
Refactoring: Making Existing Code Readable/Assessment

Practice & Assessment

Test your understanding of Refactoring: Making Existing Code Readable

Multiple Choice Questions

6
1

What makes a code change a 'refactoring' rather than a bug fix or new feature?

2

What is the purpose of a 'guard clause' in the early return pattern?

3

What does Object.freeze() accomplish when used with a constants object in JavaScript?

4

What is the 'rule of three' in the context of DRY?

5

What is a 'characterization test' in the context of refactoring?

6

When is the 'abstraction trap' triggered while applying DRY?

Coding Challenges

1
1

Refactor Nested Conditions to Guard Clauses

You are given a JavaScript file containing five functions each with 3-4 levels of nested if-else conditions. Input: the provided nested.js file containing functions for user validation, payment processing, report generation, access control, and email sending. Output: refactored.js where every function uses early return guard clauses, the maximum nesting depth is one level, and all original behavior is preserved. Include a short comment above each function describing how many levels of nesting were removed. Estimated time: 20 minutes.

Easy

Mini Project

1

Legacy Code Cleanup: Full Refactoring Pass

You are given a legacy JavaScript module (legacy-report-engine.js, approximately 300 lines) that generates sales reports. It contains: deeply nested conditions (up to 5 levels), 12 magic numbers and 8 magic strings, 4 functions over 50 lines long, 6 instances of duplicated validation logic, and 0 tests. Your task: write characterization tests first for all existing functions, then perform a complete refactoring pass — apply guard clauses to flatten nesting, replace all magic values with named constants, extract duplicated logic into shared functions, split all functions over 20 lines. Commit after each type of refactoring with a conventional commit message. Final output: refactored-report-engine.js, test file, and REFACTOR_LOG.md listing every change made and the technique applied.

Hard