Adesh
Adesh

Reputation: 947

Choosing appropriate name for methods in Java

I am a little bit confused about something and I would appreciated it if you all can bring some clarity to this. I have a class payment which has some methods and getter/setters. Do I for example use the method ItemCost to return the valve of attribute itemCost or do I use a getter?

public class Payment {
    private int itemCost, totalCost;

    public int itemCost(int itemQuantity, int itemPrice){
        itemCost = itemPrice * itemQuantity;
        return itemCost;
    }

    public int totalCost(BigDecimal itemPrice){
        totalCost = totalCost + itemCost;
        return totalCost;
    }

    public int getBalance(int clickValue, int totalCost){

        totalCost = totalCost - clickValue;
        return totalCost;
    }

    public int getTotalcost(){
        return this.totalCost;
    }    

    public void setTotalcost(int totalCost){
        this.totalCost = totalCost;
    }

    public int getItemcost(){
        return this.itemCost;
    }    

    public void setItemcost(int itemCost){
        this.itemCost = itemCost;
    }
} 

ok so instead of instantiating: int cost = payment.itemCost(quantity, itemPrice) in another class

DO: payment.itemCost(quantity, itemPrice) payment.getItemcost

?

Edit 2: Would making all the methods return void and just use the getters be better coding?

public class Payment {
    private int itemCost, totalCost;

    public void calculateItemcost(int itemQuantity, int itemPrice){
        itemCost = itemPrice * itemQuantity;
    }

    public void calculateTotalCost(BigDecimal itemPrice){
        this.totalCost = totalCost + itemCost;
    }

    public void calculateBalance(int clickValue, int totalCost){
        this.totalCost = totalCost - clickValue;
    }

    public int getTotalcost(){
        return this.totalCost;
    }    

    public void setTotalcost(int totalCost){
        this.totalCost = totalCost;
    }

    public int getItemcost(){
        return this.itemCost;
    }    

    public void setItemcost(int itemCost){
        this.itemCost = itemCost;
    }
} 

Upvotes: 0

Views: 243

Answers (3)

kosa
kosa

Reputation: 66637

getter/setters are for the purpose of setting value to particular attribute in object and getting same from the object, this way you can define attributes as private and enforce encapsulation (One of the OO principles) .

When you are doing any calculations (or) business logic, its always better to use appropriate operation name instead get/set.

EDIT:

As neel commented, Its always suggested to leave POJO as simple beans instead of stuffing in business logic/calculations. You may have another class with business logic and use get/setter to get values from POJO while doing calculations.

Upvotes: 2

Rohit Jain
Rohit Jain

Reputation: 213233

Generally, it should be possible to understand what your method is supposed to do just from the name of your method.

So, you should use getters and setters only if you want to return or set the property of your class. That way your code looks more readable, and the name of the methods clearly states what it would do.

But, if your method is not returning the exact property, but a returning the result of some calculation, then you should name your method accordingly.

For E.g: - If your method returns the cost as a calculation on some property of your class, then name that method as calculateCost. It makes more sense.

PS: - Remember, your code would me maintained for more number of time, than you took it to create. Code for others to understand, not for yourself to understand.

Upvotes: 0

The Cat
The Cat

Reputation: 2475

At the moment you have 2 methods that can set itemCost.

public void setItemcost(int itemCost){
    this.itemCost = itemCost;
}

public int itemCost(int itemQuantity, int itemPrice){
    itemCost = itemPrice * itemQuantity;
    return itemCost;
}

Ideally you would have one setting method but if you want the class to work like this I would suggest making both of these methods return void and use getItemCost to get the value.

Upvotes: 0

Related Questions