275 ການອ່ານ

Refactoring 027 - ວິທີການຕັດ Getters

ໂດຍ Maximiliano Contieri7m2025/04/20
Read on Terminal Reader

ຍາວເກີນໄປ; ອ່ານ

ການຫຼຸດຜ່ອນຫຼືການປ່ຽນແປງ getters ກັບວິທີການ behavior-rich ທີ່ເຮັດວຽກໃນຂະນະທີ່ຫຼຸດຜ່ອນສະຖານທີ່ພາຍໃນ.
featured image - Refactoring 027 - ວິທີການຕັດ Getters
Maximiliano Contieri HackerNoon profile picture

ການເຮັດວຽກຂອງ Object ຫຼັງຈາກການເຂົ້າເຖິງຂໍ້ມູນ

ພາສາລາວ

TL;DR: ດາວໂຫລດຫຼືປ່ຽນແປງ getters ກັບການປິ່ນປົວທີ່ສົມບູນແບບທີ່ເຮັດໃຫ້ການເຮັດວຽກໃນຂະນະທີ່ກວດສອບສະຖານທີ່ພາຍໃນ.

ພາສາລາວ

TL;DR: ດາວໂຫລດຫຼືປ່ຽນແປງ getters ກັບການປິ່ນປົວທີ່ສົມບູນແບບທີ່ເຮັດໃຫ້ການເຮັດວຽກໃນຂະນະທີ່ກວດສອບສະຖານທີ່ພາຍໃນ.

ລະຫັດ QR

    ພາສາລາວ
  • ໂຮງງານຜະລິດ Anemic
  • ພາສາລາວ
  • ການເຊື່ອມຕໍ່
  • ພາສາລາວ
  • ດາວນ໌ໂຫລດ Encapsulation
  • ພາສາລາວ
  • ການປ່ຽນແປງ Essence
  • ພາສາລາວ
  • ລະຫັດ QR
  • ພາສາລາວ
  • ຄວາມຄິດເຫັນ
  • ພາສາລາວ
  • ປະເພດ Internal
  • ພາສາລາວ
  • ຄວາມຄິດເຫັນທີ່ Primitive
  • ພາສາລາວ

ຊື່ຫຍໍ້ຂອງ : Code Smells

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

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

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

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

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

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

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

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

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

ປະເພດ

    ພາສາລາວ
  1. ຊື່ຫຍໍ້ຂອງ : Identify getters that expose internal object state
  2. ພາສາລາວ
  3. ດາວນ໌ໂຫລດ Codebase
  4. ພາສາລາວ
  5. ການປິ່ນປົວ Move ທີ່ນໍາໃຊ້ getter ໃນຕົວຢ່າງຕົນເອງ
  6. ພາສາລາວ
  7. Create intention-revealing methods that perform operations (ຫຼຸດຜ່ອນ get prefix)
  8. ພາສາລາວ
  9. ການປັບປຸງລະຫັດຂອງທ່ານເພື່ອນໍາໃຊ້ວິທີການໃຫມ່
  10. ພາສາລາວ

ລະຫັດຮູບແບບ

ທີ່ຜ່ານມາ

public class Invoice {
    private List<LineItem> items;
    private Customer customer;
    private LocalDate dueDate;
    
    public Invoice(Customer customer, LocalDate dueDate) {
        this.customer = customer;
        this.dueDate = dueDate;
        this.items = new ArrayList<>();
    }
    
    public void addItem(LineItem item) {
        // This is the right way 
        // to manipulate the internal consistency
        // adding assertions and access control if necessary
        items.add(item);
    }
    
    public List<LineItem> getItems() {
        // You are exposing your internal implementation
        // In some languages, you also open a backdoor to
        // manipulate your own collection unless you return
        // a copy
        return items;
    }
    
    public Customer getCustomer() {
        // You expose your accidental implementation
        return customer;
    }
    
    public LocalDate getDueDate() {
        // You expose your accidental implementation
        return dueDate;
    }
}
 
Invoice invoice = new Invoice(customer, dueDate);
// Calculate the total violating encapsulation principle
double total = 0;
for (LineItem item : invoice.getItems()) {
    total += item.getPrice() * item.getQuantity();
}

// Check if the invoice is overdue
boolean isOverdue = LocalDate.now().isAfter(invoice.getDueDate());

// Print the customer information
System.out.println("Customer: " + invoice.getCustomer().getName());

ຫຼັງຈາກ

public class Invoice {
    private List<LineItem> items;
    private Customer customer;
    private LocalDate dueDate;
    
    public Invoice(Customer customer, LocalDate dueDate) {
        this.customer = customer;
        this.dueDate = dueDate;
        this.items = new ArrayList<>();
    }
    
    public void addItem(LineItem item) {
        items.add(item);
    }
    
    // Step 3: Move behavior that uses the getter into the object
    public double calculateTotal() {
        // Step 4: Create intention-revealing methods
        double total = 0;
        for (LineItem item : items) {
            total += item.price() * item.quantity();
        }
        return total;
    }
    
    public boolean isOverdue(date) {
        // Step 4: Create intention-revealing methods
        // Notice you inject the time control source
        // Removing the getter and breaking the coupling
        return date.isAfter(dueDate);
    }
    
    public String customerInformation() {
        // Step 4: Create intention-revealing methods
        // You no longer print with side effects 
        // And coupling to a global console
        return "Customer: " + customer.name();        
    }
    
    // For collections, return an unmodifiable view if needed
    // Only expose internal collaborators if the name 
    // is an actual behavior
    public List<LineItem> items() {
        return Collections.unmodifiableList(items);
    }
    
    // Only if required by frameworks 
    // or telling the customer is an actual responsibility
    // The caller should not assume the Invoice is actually
    // holding it
    public String customerName() {
        return customer.name();
    }
    
    // You might not need to return the dueDate
    // Challenge yourself if you essentially need to expose it
    // public LocalDate dueDate() {
    //     return dueDate;
    // }
}

// Client code (Step 5: Update client code)
Invoice invoice = new Invoice(customer, dueDate);
double total = invoice.calculateTotal();
boolean isOverdue = invoice.isOverdue(date);
System.out.println(invoice.customerInformation());

ປະເພດ

    ພາສາລາວ
  • [x] ອັດຕະໂນມັດ
  • ພາສາລາວ

ຄວາມປອດໄພ️

ການ refactoring ນີ້ແມ່ນປົກກະຕິໂດຍບໍ່ມີຄວາມປອດໄພ, ແຕ່ຈໍາເປັນຕ້ອງປະຕິບັດຢ່າງກວ້າງຂວາງ.


ທ່ານຈໍາເປັນຕ້ອງຮັບປະກັນການນໍາໃຊ້ທັງຫມົດຂອງ getter ໄດ້ຖືກເຂົ້າລະຫັດແລະປ່ຽນແປງກັບວິທີການຄຸ້ມຄອງໃຫມ່.


ຄວາມປອດໄພທີ່ໃຫຍ່ທີ່ສຸດແມ່ນໃນເວລາທີ່ getters returns Objects or Collections ທີ່ສາມາດປ່ຽນແປງ, ໃນຂະນະທີ່ລະຫັດໂທລະສັບມືຖືສາມາດປ່ຽນແປງ Objects ນີ້.


ທ່ານຕ້ອງກວດສອບວ່າການປະຕິບັດໄດ້ບໍ່ໄດ້ປ່ຽນແປງໂດຍຜ່ານການທົດສອບຢ່າງກວ້າງຂວາງກ່ອນແລະຫຼັງຈາກ refactoring.


ຖ້າຫາກວ່າທ່ານກໍາລັງຊອກຫາຂໍ້ມູນເພີ່ມເຕີມ, ກະລຸນາເລືອກເອົາຂໍ້ມູນເພີ່ມເຕີມກ່ຽວກັບການເຂົ້າເຖິງຂໍ້ມູນເພີ່ມເຕີມ.


ໃນຖານະເປັນປົກກະຕິ, ທ່ານຄວນເພີ່ມການປົກປັກຮັກສາ behavioral (ບໍ່ແມ່ນການກໍ່ສ້າງ) ກັບລະຫັດຂອງທ່ານກ່ອນທີ່ຈະເຮັດການ refactoring.

ເປັນຫຍັງ Code ທີ່ດີທີ່ສຸດ?

ລະຫັດ refactored ເປັນທີ່ດີທີ່ສຸດສໍາລັບການວ່າມັນແມ່ນລວມກັບຊື່ຫຍໍ້ຂອງ : Tell Don't Askວິທີການ, ເຮັດໃຫ້ Objects ຂອງທ່ານ smart rather than just anemic data holders.


ການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວຂອງການປິ່ນປົວ


ວິທີການນີ້ຄວນ reducedລະຫັດ QRໃນຂະນະທີ່ລູກຄ້າບໍ່ຈໍາເປັນຕ້ອງຮູ້ກ່ຽວກັບການກໍ່ສ້າງພາຍໃນຂອງອຸປະກອນ.


ມັນຍັງກວດສອບການເຄື່ອນໄຫວຂອງຄໍາສັ່ງ Demeter ໂດຍ eliminating ສາຍຂອງ getters.


ຫຼັງຈາກລະຫັດ QR, ການແກ້ໄຂສາມາດຮັບປະກັນການຢັ້ງຢືນທີ່ດີກວ່າແລະການປະຕິບັດການຄຸ້ມຄອງຄຸນນະພາບຂອງບໍລິສັດໃນອຸປະກອນ.

ວິທີການປັບປຸງ bijection? ️

ການອັບໂຫລດ getters ເຮັດໃຫ້ການປັບປຸງປະເພດ Bijectionລະຫັດແລະຄຸນນະພາບໂດຍເຮັດໃຫ້ Objects ເຮັດໃຫ້ເປັນຕົວແທນຂອງພວກເຂົາໃນໂລກທີ່ແທ້ຈິງ.


ໃນໂລກທີ່ແທ້ຈິງ, Objects ບໍ່ເຂົ້າໃຈກັບສະຖານະການຂອງເຂົາເຈົ້າສໍາລັບຜູ້ອື່ນໆເພື່ອທົດສອບ - ພວກເຂົາເຈົ້າເຮັດວຽກໂດຍຜ່ານການຢັ້ງຢືນ.


ວິທີການດາວໂຫລດແລະດາວໂຫລດອຸປະກອນການໃນອິນເຕີເນັດ


ທ່ານສາມາດສ້າງການສະແດງທີ່ດີກວ່າຂອງຄຸນນະສົມບັດຂອງຄຸນນະສົມບັດຂອງຄຸນນະສົມບັດໂດຍ modeling Objects ຂອງທ່ານເພື່ອເຮັດວຽກໃນຂະນະທີ່ບໍ່ເຂົ້າໃຈກັບຂໍ້ມູນ.

ນີ້ເຮັດໃຫ້ການຕັດສິນໃຈຫນຶ່ງກັບຫນຶ່ງລະຫວ່າງໂລກທີ່ແທ້ຈິງແລະມາດຕະຖານຄອມພິວເຕີຂອງທ່ານ, ເຮັດໃຫ້ລະຫັດຂອງທ່ານຢ່າງງ່າຍດາຍຫຼາຍແລະເຫມາະສົມກັບວິທີທີ່ຜູ້ຊ່ຽວຊານຊອກຫາກ່ຽວກັບພື້ນຖານບັນຫາ.


ວິທີການນີ້ແມ່ນລວມເອົາການພາສາລາວການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບການຄາດຄະເນດິນດີຕ້ອນຮັບ

ລະຫັດ QR

Frameworks ແລະ libraries ຫຼາຍກວ້າງຂວາງຄາດວ່າຈະມີວິທີການ getter ສໍາລັບ serialization / deserialization.


ລະບົບ codebases Legacy ສາມາດມີການນໍາໃຊ້ getter ທີ່ຖືກນໍາໃຊ້ຢ່າງກວ້າງຂວາງທີ່ມັນເປັນຄວາມເຂັ້ມແຂງເພື່ອ refactor ທັງຫມົດໃນຂະນະທີ່ຫນຶ່ງ.


ການທົດສອບອຸປະກອນສາມາດໄດ້ຮັບການປະຫວັດສາດເພີ່ມເຕີມໃນຂະນະທີ່ສະຖານທີ່ພາຍໃນແມ່ນບໍ່ສາມາດເຂົ້າເຖິງ. ຂໍຂອບໃຈວ່າທ່ານບໍ່ຈໍາເປັນຕ້ອງທົດສອບວິທີສ່ວນບຸກຄົນ.

Refactor ກັບ AI

ພາສາລາວ

ການນໍາສະເຫນີ Prompt: 1. identify getters that expose internal object state 2. Find all getter uses in the codebase 3. Move behavior that uses the getter into the object itself 4. Create intention-revealing methods that perform operations (remove the get prefix) 5. Update your code to use the new methods

ພາສາລາວ

ການນໍາສະເຫນີ Prompt: 1. identify getters that expose internal object state 2. Find all getter uses in the codebase 3. Move behavior that uses the getter into the object itself 4. Create intention-revealing methods that perform operations (remove the get prefix) 5. Update your code to use the new methods

ພາສາລາວຄໍາແນະນໍາທີ່ດີກັບຄໍາແນະນໍາພິເສດ ChatGPT ChatGPT Claude Claude Perplexity Perplexity Copilot Copilot Gemini Gemini DeepSeek DeepSeek Meta AI Meta AI Grok Grok Qwen Qwen
ຄໍາແນະນໍາທີ່ດີກັບຄໍາແນະນໍາພິເສດ ChatGPT ChatGPT Claude Claude Perplexity Perplexity Copilot Copilot Gemini Gemini DeepSeek DeepSeek Meta AI Meta AI Grok Grok Qwen Qwenບໍ່ມີຄໍາແນະນໍາທີ່ເຫມາະສົມທີ່ມີຄໍາແນະນໍາພິເສດພາສາລາວ

ວິທີການທີ່ດີເລີດ

ວິທີການທີ່ດີເລີດ

ການຝຶກອົບຮົມ

ການຝຶກອົບຮົມ

ພາສາລາວລະຫັດ QRພາສາລາວລະຫັດ QRພາສາລາວລະຫັດ QR

ລະຫັດ QR

ພາສາລາວ

ລະຫັດ QR

ລະຫັດ QR

ມັງກອນ Claudeລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QR

ພາສາລາວປະຫວັດສາດພາສາລາວປະຫວັດສາດພາສາລາວປະຫວັດສາດ

ປະຫວັດສາດ

ພາສາລາວ

ປະຫວັດສາດ

ປະຫວັດສາດ

ຄົ້ນຫາ Copilotຄູ່ມື

ຄູ່ມື

ຄູ່ມື

ຄູ່ມື

ປະເພດ Geminiປະເພດ

ປະເພດ

ປະເພດ

ປະເພດ

ດາວນ໌ໂຫລດ DeepSeekດາວໂຫລດ DeepSeek

ດາວໂຫລດ DeepSeek

ດາວໂຫລດ DeepSeek

ດາວໂຫລດ DeepSeek

ຊື່ຫຍໍ້ຂອງ : Meta AIພາສາລາວ

ພາສາລາວ

ພາສາລາວ

ພາສາລາວ

ລະຫັດ QRລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QRລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QR

ລະຫັດ QR

ມື້ ️

    ພາສາລາວ
  • ລະຫັດ QR
  • ພາສາລາວ

ປະເພດ

    ພາສາລາວ
  • [x] ອັດຕະໂນມັດ
  • ພາສາລາວ

ຄວາມຄິດເຫັນທີ່ Refactoring

ຂ້າ ພະ ເຈົ້າ

ລະຫັດ QR

ຮູບພາບການເດີນທາງປະເພດປະເພດ Pixabay


ບົດຄວາມນີ້ແມ່ນສ່ວນຫນຶ່ງຂອງ Series Refactoring.


L O A D I N G
. . . comments & more!

About Author

Maximiliano Contieri HackerNoon profile picture
Maximiliano Contieri@mcsee
I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written

ວາງປ້າຍ

ບົດ​ຄວາມ​ນີ້​ໄດ້​ຖືກ​ນໍາ​ສະ​ເຫນີ​ໃນ...

Trending Topics

blockchaincryptocurrencyhackernoon-top-storyprogrammingsoftware-developmenttechnologystartuphackernoon-booksBitcoinbooks