Refactoring 016 – Building With The Essence

Pass essential attributes during object creation to reduce mutability and eliminate getters and setters. Remove setter and getter methods. Update object creation calls to pass all required attributes upfront. Use the following code snippets to help you understand the code. The code is available on Hackernoon.


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

Building Immutable foundations from the ground

\

TL;DR: Pass essential attributes during object creation to reduce mutability and eliminate getters and setters.

Problems Addressed

Steps

  1. Identify essential attributes required for object creation
  2. Create a constructor that accepts all essential attributes
  3. Remove setter and getter methods
  4. Update object creation calls to pass all required attributes upfront

Sample Code

Before

public class CreditCard {
    private String cardNumber;
    private String cardHolderName;
    private String expirationMonthYear;
    private int cvv;

    public CreditCard() {} // Empty Constructor

    public void setCardNumber(String cardNumber) { 
        this.cardNumber = cardNumber; 
    }
    public void setCardHolderName(String cardHolderName) { 
        this.cardHolderName = cardHolderName; 
    }
    public void setExpirationMonthYear(String expirationMonthYear) { 
        this.expirationMonthYear = expirationMonthYear;
    }
    public void setCvv(int cvv) { 
        this.cvv = cvv; 
    }

    public String getCardNumber() { 
        return cardNumber; 
    }
    public String getCardHolderName() {
        return cardHolderName;
    }
    public String getExpirationMonthYear() {
        return expirationMonthYear; 
    }
    public int getCvv() {
        return cvv; 
    }
}

CreditCard card = new CreditCard();
card.setCardNumber("1234-5678-9012-3456");
card.setCardHolderName("Lilywhite Lilith");
card.setExpirationMonthYear("12/25");
card.setCvv(123);

After

public class CreditCard {
    private final String cardNumber;
    private final String cardHolderName;
    private final String expirationMonthYear;
    private final int cvv;

    public CreditCard(String cardNumber,
                      String cardHolderName,
                      String expirationMonthYear,
                      int cvv) {
        // 1. Identify essential attributes for object creation
        // 2. Create a constructor that accepts all essential attributes
        this.cardNumber = cardNumber;
        this.cardHolderName = cardHolderName;
        this.expirationMonthYear = expirationMonthYear;
        this.cvv = cvv;
    }

    // 3. Remove setter and getter methods 

    // Find real behavior related to credit card usage

}

// 4. Update object creation calls to pass all required attributes upfront
CreditCard card = new CreditCard("1234-5678-9012-3456",
                                 "Lilywhite Lilith", 
                                 "12/25", 
                                 123);

Type

  • Semi-Automatic

\ This is a step-by-step refactor.

Safety

This refactoring is generally safe if you ensure you pass all essential attributes during object creation.

You must update all object creation sites, which may require refactoring tools and careful review in larger codebases.

Why is the code better?

The refactored code enforces object integrity by requiring all essential attributes at creation time.

\ The objects become thread-safe reducing the risk of being in an inconsistent state.

AI Correction

Most AI tools can correct this code with explicit instructions.

You can have clear pre-prompts requesting all your code samples to favor immutability.

Try Them!

Remember AI Assistants make lots of mistakes

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

Tags

  • Mutability

Related Refactorings

Refactoring 001 - Remove Setters

See also

https://hackernoon.com/nude-models-part-i-setters-5tm3u3y?embedable=true

https://hackernoon.com/nude-models-part-ii-getters-sjo3ua2?embedable=true

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

Credits

Image by Denis on Pixabay


This article is part of the Refactoring Series.

How to Improve your Code With Easy Refactorings


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


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2024-09-16T14:19:45+00:00) Refactoring 016 – Building With The Essence. Retrieved from https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/

MLA
" » Refactoring 016 – Building With The Essence." Maximiliano Contieri | Sciencx - Monday September 16, 2024, https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/
HARVARD
Maximiliano Contieri | Sciencx Monday September 16, 2024 » Refactoring 016 – Building With The Essence., viewed ,<https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Refactoring 016 – Building With The Essence. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/
CHICAGO
" » Refactoring 016 – Building With The Essence." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/
IEEE
" » Refactoring 016 – Building With The Essence." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/. [Accessed: ]
rf:citation
» Refactoring 016 – Building With The Essence | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/09/16/refactoring-016-building-with-the-essence/ |

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.