This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getters as well  RSS feed

 
A Alqtn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

Back to basics. A getter method return a value for an instance variable. For example the following getter method returns a price

public float getPrice( ) {
return price;
}

Now if I wish this get method to return a formated price, is it suitable here to have the getter method return the formated value or it is better to do the formating at the place where the price has been received. As an example let us say that I want to format the price to UK locale

public String getPrice( ) {
Locale ukLocale = new Locale("en", "uk");
NumberFormat nfmt = NumberFormat.getCurrencyInstance( );

return nfmt.format( price );
}

This getter method is returning the price (which is actually of a float type) as a formated String. Is this good here or not? I am looking for best practices here (not it depends on the application answer).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Alqtn, please have a look at the JavaRanch naming policy and change your display name accordingly.

I would separate the formatting from the getter method (i.e., do not do the formatting in the getter method).

Think about the MVC (Model, View, Controller) architecture. It's desirable to keep the user interface part (the view) separate from the business logic and data in the application. Formatting is something that belongs to the view, so you shouldn't put formatting in the data objects (the model).
 
A Alqtn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does this apply to the toSting( ) method? Should toString( ) return plain data without formatting or the story is different here?
 
A Alqtn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper, the naming policy says "You can even use initials for the first name if you like." So what is wrong with the name I am using?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by A Alqtn:
Jesper, the naming policy says "You can even use initials for the first name if you like." So what is wrong with the name I am using?


The initial is OK, but the last name is not a pronounceable word.

Anyway, as to your question: getters should return the most useful form of the information, which in this case is the price as a number (but more about that below...) Returning the price as a formatted string is inconvenient for any code that needs to add a list of prices, for example. Formatting for presentation is up to some other class.

About toString(): unless a class is documented otherwise, you can assume that toString() returns something useful for debugging/development only. It's fine, though, to make it return anything you want, as long as you document what it returns.

Now, finally, about using a float to represent money: don't do it. floats are not exact, and in a real application, at least, people tend to be very picky about monetary calculations being exact. Even being off by a penny is unacceptable (did you see the movie "Office Space?") Instead, you should use either an int to hold the number of pennies, a BigDecimal, or (best of all, in many cases) a Money class, either one you write or an existing one you obtain from a third party.
 
A Alqtn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


The initial is OK, but the last name is not a pronounceable word.

Anyway, as to your question: getters should return the most useful form of the information, which in this case is the price as a number (but more about that below...) Returning the price as a formatted string is inconvenient for any code that needs to add a list of prices, for example. Formatting for presentation is up to some other class.

About toString(): unless a class is documented otherwise, you can assume that toString() returns something useful for debugging/development only. It's fine, though, to make it return anything you want, as long as you document what it returns.

Now, finally, about using a float to represent money: don't do it. floats are not exact, and in a real application, at least, people tend to be very picky about monetary calculations being exact. Even being off by a penny is unacceptable (did you see the movie "Office Space?") Instead, you should use either an int to hold the number of pennies, a BigDecimal, or (best of all, in many cases) a Money class, either one you write or an existing one you obtain from a third party.


-------------------------------------------------

Thanks Enrest for the valuable information. Regarding my last name it is not English that is why it is not pronouceable.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(did you see the movie "Office Space?")


Was that 'Office Space'? I thought that was Superman III.
[ January 20, 2006: Message edited by: Garrett Rowe ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!