Last active
October 5, 2025 18:39
-
-
Save mcsee/bd6e1bf62f3a6a3e0843bba573cc7638 to your computer and use it in GitHub Desktop.
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 2. Create a meaningful entity to group them | |
| class PriceRange { | |
| constructor(public min: Currency, public max: Currency) { | |
| if (min > max) { | |
| throw new Error( | |
| `Invalid price range: min (${min}) `+ | |
| `cannot be greater than max (${max})` | |
| ); | |
| } | |
| if (min < 0) { | |
| throw new Error( | |
| `Invalid price range: min (${min}) cannot be negative`); | |
| } | |
| } | |
| } | |
| class Interval { | |
| // 3. Add missing validation rules to fail-fast | |
| constructor(public start: Date, public end: Date) { | |
| if (start > end) { | |
| throw new Error( | |
| `Invalid date range: start (${start.toISOString()}) ` + | |
| `cannot be after end (${end.toISOString()})` | |
| ); | |
| } | |
| } | |
| } | |
| class HolidaySearchCriteria { | |
| constructor( | |
| public priceRange: PriceRange, | |
| public dateRange: Interval | |
| ) {} | |
| } | |
| function findHolidays(criteria: HolidaySearchCriteria): Holiday[] { | |
| // 1. Identify multiple parameters of the same type | |
| // No need to call validate() - already validated in constructors | |
| // 4. Replace function signatures with the new entity | |
| const { priceRange, dateRange } = criteria; | |
| // 5. Adjust all callers to pass the entity | |
| // 6. Add context-specific names to improve clarity | |
| return database.query({ | |
| price: { $gte: priceRange.min, $lte: priceRange.max }, | |
| startDate: { $gte: dateRange.start }, | |
| endDate: { $lte: dateRange.end } | |
| }); | |
| } | |
| try { | |
| const criteria = new HolidaySearchCriteria( | |
| new PriceRange(500, 1000), // ✅ Valid | |
| new Inteval( | |
| new Date('2025-06-01'), | |
| new Date('2025-06-15') | |
| ) | |
| ); | |
| findHolidays(criteria); | |
| // ❌ This will throw immediately | |
| // Great for UI and API validation | |
| new PriceRange(1000, 500); | |
| } catch (error) { | |
| console.error(error.message); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment