zkytony
zkytony

Reputation: 1508

Best Java practices - When should one add a field in a Java Class?

When is the right time to add another field to a Java Class? For example, if I keep track of a 'sum' property of a class, should I use a field to store the current sum, or should I add up the sum when client calls sum()?

To clarify the question,

suppose I have a class called ShoppingList, it has a function called getTotalPrice() which returns total price on the particular shopping list; it also has functions addItem() and removeItem(), which will modify the shopping item objects on this list (these objects has a field called price).

To implement the getTotalPrice, should I have another field totalPrice to keep track of the current total price, or should I sum up all items' prices in the shopping list whenever the client calls getTotalPrice()?

Items may be stored in a collection.

Upvotes: 2

Views: 852

Answers (4)

dln385
dln385

Reputation: 12110

This is a question of best practices. “Don't repeat yourself”—and more specifically “Single Source of Truth”—contend that information should be stored only once. If the sum can be calculated from other fields, it should not be stored as a separate field. In practice, a field should be cached if it is expensive to compute. Special care must be taken, however, if the underlying information can be changed, thus causing the cache to become incorrect.

Upvotes: 4

misko321
misko321

Reputation: 493

Obviously, when you're going to use that value in more than one method (e.g. you've calculated the sum in one method, but need to access it in another one) and you don't want to send it as a parameter (for example, if method B uses several values calculated by method A and you don't want to have a long list of arguments in B's definition).

Furthermore - if it takes long to calculate the sum. There's not much sense in calling it every time it is accessed, with sum() which may be time consuming (unless it changes so fast, that you do want to recalculate it, which doesn't seem to be the case in general).

This applies also to your example with shopping list. Shopping card will usually contains just a few products, so calculating total price doesn't take long. In that case, I wouldn't rather store it in a class' field.

Upvotes: 0

Shahzad
Shahzad

Reputation: 2073

Whenever you need to store and use a value globally, that is the right time to add another field to a Java class.

For example, if you have a calculator and you want to show the sum of the user's calculation, you would just use a method, sum(), to do the calculation and return the sum of the calculation which can then be used to show the sum. So in this case you wouldn't need a sum field.

Upvotes: 0

Johnny Willer
Johnny Willer

Reputation: 3927

Depends on situation, if sum() is called many times and there is a large calculation inside it, then is preferable that you store in a variable, but if the calculation is not so big and sum() is not called many times, you can just only return the result of the calculation as well.

Upvotes: 0

Related Questions