| 1 | // A well-structured todo list manager following SOLID principles |
| 2 | // Refactored solution for next-edit-3-2.ts |
| 3 | |
| 4 | // Define types for better maintainability |
| 5 | type Priority = "low" | "medium" | "high"; |
| 6 | |
| 7 | interface Todo { |
| 8 | id: number; |
| 9 | text: string; |
| 10 | completed: boolean; |
| 11 | priority: Priority; |
| 12 | } |
| 13 | |
| 14 | // TodoManager class following Single Responsibility Principle |
| 15 | class TodoManager { |
| 16 | private todos: Map<number, Todo> = new Map(); |
| 17 | private lastId: number = 0; |
| 18 | |
| 19 | // Create a new todo |
| 20 | addTodo(text: string, priority: Priority = "medium"): Todo { |
| 21 | // Input validation |
| 22 | if (!text.trim()) { |
| 23 | throw new Error("Todo text cannot be empty"); |
| 24 | } |
| 25 | |
| 26 | const newTodo: Todo = { |
| 27 | id: ++this.lastId, |
| 28 | text: text.trim(), |
| 29 | completed: false, |
| 30 | priority, |
| 31 | }; |
| 32 | |
| 33 | this.todos.set(newTodo.id, newTodo); |
| 34 | return { ...newTodo }; // Return a copy to prevent direct modification |
| 35 | } |
| 36 | |
| 37 | // Delete a todo by id |
| 38 | deleteTodo(id: number): boolean { |
| 39 | return this.todos.delete(id); |
| 40 | } |
| 41 | |
| 42 | // Toggle todo completion status |
| 43 | toggleTodo(id: number): Todo | null { |
| 44 | const todo = this.todos.get(id); |
| 45 | if (!todo) return null; |
| 46 | |
| 47 | const updatedTodo = { ...todo, completed: !todo.completed }; |
| 48 | this.todos.set(id, updatedTodo); |
| 49 | return { ...updatedTodo }; |
| 50 | } |
| 51 | |
| 52 | // Update todo text |
| 53 | updateTodoText(id: number, newText: string): Todo | null { |
| 54 | if (!newText.trim()) { |
| 55 | throw new Error("Todo text cannot be empty"); |
| 56 | } |
| 57 | |
| 58 | const todo = this.todos.get(id); |
| 59 | if (!todo) return null; |
| 60 | |
| 61 | const updatedTodo = { ...todo, text: newText.trim() }; |
| 62 | this.todos.set(id, updatedTodo); |
| 63 | return { ...updatedTodo }; |
| 64 | } |
| 65 | |
| 66 | // Change todo priority |
| 67 | changePriority(id: number, newPriority: Priority): Todo | null { |
| 68 | const todo = this.todos.get(id); |
| 69 | if (!todo) return null; |
| 70 | |
| 71 | const updatedTodo = { ...todo, priority: newPriority }; |
| 72 | this.todos.set(id, updatedTodo); |
| 73 | return { ...updatedTodo }; |
| 74 | } |
| 75 | |
| 76 | // Get all todos |
| 77 | getAllTodos(): Todo[] { |
| 78 | return Array.from(this.todos.values()).map((todo) => ({ ...todo })); |
| 79 | } |
| 80 | |
| 81 | // Filter todos by various criteria (following Open/Closed Principle) |
| 82 | filterTodos(predicate: (todo: Todo) => boolean): Todo[] { |
| 83 | return this.getAllTodos().filter(predicate); |
| 84 | } |
| 85 | |
| 86 | // Helper methods built on top of filterTodos |
| 87 | getCompletedTodos(): Todo[] { |
| 88 | return this.filterTodos((todo) => todo.completed); |
| 89 | } |
| 90 | |
| 91 | getActiveTodos(): Todo[] { |
| 92 | return this.filterTodos((todo) => !todo.completed); |
| 93 | } |
| 94 | |
| 95 | getTodosByPriority(priority: Priority): Todo[] { |
| 96 | return this.filterTodos((todo) => todo.priority === priority); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Create a singleton instance |
| 101 | const todoManager = new TodoManager(); |
| 102 | |
| 103 | // Example usage |
| 104 | todoManager.addTodo("Buy groceries", "high"); |
| 105 | todoManager.addTodo("Clean house"); |
| 106 | todoManager.addTodo("Pay bills", "high"); |
| 107 | todoManager.toggleTodo(2); |
| 108 | |
| 109 | console.log("All todos:", todoManager.getAllTodos()); |
| 110 | console.log("High priority todos:", todoManager.getTodosByPriority("high")); |
| 111 | console.log("Completed todos:", todoManager.getCompletedTodos()); |
| 112 | |
| 113 | // Export the TodoManager class and its instance |
| 114 | export { Priority, Todo, TodoManager, todoManager }; |
| 115 | |
| 116 | /* |
| 117 | Code Smells in next-edit-3-2.ts: |
| 118 | Global state - using global variables for todos and lastId |
| 119 | Single Responsibility Principle violation - mixing data management with business logic |
| 120 | Duplicated code in several functions that iterate through todos |
| 121 | No clear abstraction layers - direct manipulation of the data structure |
| 122 | No validation or error handling |
| 123 | No proper typing for priority values |
| 124 | No encapsulation - direct access to the todos array |
| 125 | No use of more efficient data structures (using array iteration instead of maps/objects) |
| 126 | |
| 127 | Improvements Made in the Refactored Solution: |
| 128 | |
| 129 | Encapsulation and Data Hiding |
| 130 | |
| 131 | Created a TodoManager class to encapsulate all operations and data |
| 132 | Made todos and lastId private members of the class |
| 133 | Used a Map instead of an array for more efficient lookup by id |
| 134 | |
| 135 | |
| 136 | Proper Type Definitions |
| 137 | |
| 138 | Defined a proper Todo interface |
| 139 | Created a union type for Priority with specific allowed values |
| 140 | |
| 141 | |
| 142 | Single Responsibility Principle |
| 143 | |
| 144 | Each method has a clear, single responsibility |
| 145 | Separated data structure management from business logic |
| 146 | |
| 147 | |
| 148 | Open/Closed Principle |
| 149 | |
| 150 | Introduced a general filterTodos method that accepts a predicate |
| 151 | Built specialized filter methods on top of this generic one |
| 152 | |
| 153 | |
| 154 | Data Integrity and Validation |
| 155 | |
| 156 | Added validation for todo text to prevent empty todos |
| 157 | Return copies of todos to prevent direct modification of internal state |
| 158 | |
| 159 | |
| 160 | Reduced Code Duplication |
| 161 | |
| 162 | Eliminated repeated code for finding todos by id |
| 163 | Consolidated filtering logic into a single method |
| 164 | |
| 165 | |
| 166 | Improved Performance |
| 167 | |
| 168 | Used a Map data structure for O(1) lookups by id instead of O(n) array iterations |
| 169 | More efficient todo retrieval and manipulation |
| 170 | |
| 171 | |
| 172 | Better API Design |
| 173 | |
| 174 | Added more helper methods for common operations |
| 175 | Created a consistent interface with descriptive method names |
| 176 | Added a method to get all todos instead of directly accessing the array |
| 177 | |
| 178 | |
| 179 | Error Handling |
| 180 | |
| 181 | Added basic input validation with meaningful error messages |
| 182 | |
| 183 | |
| 184 | Immutability |
| 185 | |
| 186 | Returned copies of objects rather than references to internal state |
| 187 | Used object spread to create new objects when updating |
| 188 | */ |