Favor immutability by converting attributes to sets
TL;DR: Using sets for attributes simplifies your code and makes state management easier
class Bill { amount: number; paid: boolean; constructor(amount: number) { this.amount = amount; this.paid = false; } pay() { if (!this.paid) { this.paid = true; } } } const bill = new Bill(100); console.log(bill.paid); // false bill.pay(); console.log(bill.paid); // true
// 1. Identify attributes representing states class Accountant { // 2. Replace the attributes with sets: one for each state unpaidBills: Set<Bill>; paidBills: Set<Bill>; constructor() { this.unpaidBills = new Set(); this.paidBills = new Set(); } addBill(bill: Bill) { this.unpaidBills.add(bill); } payBill(bill: Bill) { // 3. Adjust methods to move items // between sets instead of mutating attributes if (this.unpaidBills.has(bill)) { this.unpaidBills.delete(bill); this.paidBills.add(bill); } } } class Bill { amount: number; constructor(amount: number) { this.amount = amount; } } const bill = new Bill(100); const accountant = new Accountant(); accountant.addBill(bill); console.log(accountant.unpaidBills.has(bill)); // true accountant.payBill(bill); console.log(accountant.paidBills.has(bill)); // true
[X] Semi-Automatic
This refactoring is safe when your attributes don't rely on specific indexing behavior.
Since sets don't maintain element order, check if your logic depends on order.
Entities are immutable in the essence.
Using sets ensures uniqueness and simplifies logic.
You no longer need to check for duplicates before adding elements.
Operations like union, intersection, and difference become straightforward, making your code more maintainable and flexible.
Sets don't preserve element order.
If your logic depends on sequence, converting to a set may not be appropriate and you should use an Ordered Collection or Array
You can prompt your AI assistants to make this refactoring for you.
Without Proper Instructions | With Specific Instructions |
---|---|
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
https://dev.to/mcsee/refactoring-001-remove-setters-26cg
Image by Angelo Giordano in Pixabay
This article is part of the Refactoring Series.
The above is the detailed content of Refactoring - Convert Attributes to Sets. For more information, please follow other related articles on the PHP Chinese website!