Refactoring 017 – Convert Attributes to Sets

Using sets for attributes simplifies your code and makes state management easier


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Favor immutability by converting attributes to sets

TL;DR: Using sets for attributes simplifies your code and makes state management easier

Problems Addressed

  • Mutability
  • Complexity
  • Attributes become polluted
  • Setters

Related Code Smells

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vii-8dk31x0

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vi-cmj31om

Steps

  1. Identify attributes representing states
  2. Replace the attributes with sets: one for each state
  3. Adjust methods to move items between sets instead of mutating attributes

Sample Code

Before

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

After

// 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();
manager.addBill(bill);
console.log(accountant.unpaidBills.has(bill)); // true
manager.payBill(bill);
console.log(accountant.paidBills.has(bill)); // true

Type

  • [x] Semi-Automatic

Safety

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.

Why is the code better?

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.

Limitations

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

AI Refactoring

You can prompt your AI assistants to make this refactoring for you.

Try Them!

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini |

Tags

  • Mutability

Related Refactorings

https://hackernoon.com/refactoring-remove-setters-codesmell?embedable=true

See also

https://hackernoon.com/is-it-crystal-clear-for-everybody-that-a-date-should-not-mutate-wuoy3z03?embedable=true

Credits

Image by Angelo Giordano in Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2024-10-21T17:36:30+00:00) Refactoring 017 – Convert Attributes to Sets. Retrieved from https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/

MLA
" » Refactoring 017 – Convert Attributes to Sets." Maximiliano Contieri | Sciencx - Monday October 21, 2024, https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/
HARVARD
Maximiliano Contieri | Sciencx Monday October 21, 2024 » Refactoring 017 – Convert Attributes to Sets., viewed ,<https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Refactoring 017 – Convert Attributes to Sets. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/
CHICAGO
" » Refactoring 017 – Convert Attributes to Sets." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/
IEEE
" » Refactoring 017 – Convert Attributes to Sets." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/. [Accessed: ]
rf:citation
» Refactoring 017 – Convert Attributes to Sets | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/10/21/refactoring-017-convert-attributes-to-sets-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.