Elffuss BraidHub

kikocis/continue public

main · 5.1 KiBRawPermalink
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
5type Priority = "low" | "medium" | "high";
6
7interface Todo {
8 id: number;
9 text: string;
10 completed: boolean;
11 priority: Priority;
12}
13
14// TodoManager class following Single Responsibility Principle
15class 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
101const todoManager = new TodoManager();
102
103// Example usage
104todoManager.addTodo("Buy groceries", "high");
105todoManager.addTodo("Clean house");
106todoManager.addTodo("Pay bills", "high");
107todoManager.toggleTodo(2);
108
109console.log("All todos:", todoManager.getAllTodos());
110console.log("High priority todos:", todoManager.getTodosByPriority("high"));
111console.log("Completed todos:", todoManager.getCompletedTodos());
112
113// Export the TodoManager class and its instance
114export { Priority, Todo, TodoManager, todoManager };
115
116/*
117Code Smells in next-edit-3-2.ts:
118Global state - using global variables for todos and lastId
119Single Responsibility Principle violation - mixing data management with business logic
120Duplicated code in several functions that iterate through todos
121No clear abstraction layers - direct manipulation of the data structure
122No validation or error handling
123No proper typing for priority values
124No encapsulation - direct access to the todos array
125No use of more efficient data structures (using array iteration instead of maps/objects)
126
127Improvements Made in the Refactored Solution:
128
129Encapsulation and Data Hiding
130
131Created a TodoManager class to encapsulate all operations and data
132Made todos and lastId private members of the class
133Used a Map instead of an array for more efficient lookup by id
134
135
136Proper Type Definitions
137
138Defined a proper Todo interface
139Created a union type for Priority with specific allowed values
140
141
142Single Responsibility Principle
143
144Each method has a clear, single responsibility
145Separated data structure management from business logic
146
147
148Open/Closed Principle
149
150Introduced a general filterTodos method that accepts a predicate
151Built specialized filter methods on top of this generic one
152
153
154Data Integrity and Validation
155
156Added validation for todo text to prevent empty todos
157Return copies of todos to prevent direct modification of internal state
158
159
160Reduced Code Duplication
161
162Eliminated repeated code for finding todos by id
163Consolidated filtering logic into a single method
164
165
166Improved Performance
167
168Used a Map data structure for O(1) lookups by id instead of O(n) array iterations
169More efficient todo retrieval and manipulation
170
171
172Better API Design
173
174Added more helper methods for common operations
175Created a consistent interface with descriptive method names
176Added a method to get all todos instead of directly accessing the array
177
178
179Error Handling
180
181Added basic input validation with meaningful error messages
182
183
184Immutability
185
186Returned copies of objects rather than references to internal state
187Used object spread to create new objects when updating
188*/