What this method should do is return a String
that displays how many albums and songs there are. This method is a private
method of my Artist
class which has a method called albumCount()
and songCount()
. The whole class probably needs an improvement but I will try that later. First I'd like this single method to be improved if possible.
public String toAlbumSongCount() {
StringBuilder builder = new StringBuilder();
final int albumCount = albumCount();
final int songCount = songCount();
builder.append( albumCount );
//Append albums instead of album if albumCount is greater than 1
builder.append( (albumCount > 1) ? " albums" : " album" );
builder.append( "," ); //Move this to the line above?
builder.append( songCount );
//Append songs instead of song if songCount is greater than 1
builder.append( (songCount > 1 ) ? " songs" : " song" );
return builder.toString();
}
I used the StringBuilder
class because I heard a dozen of times that it will improve performance when concatenating Strings.
I wasn't sure about those two lines :
builder.append( (albumCount > 1) ? " albums" : " album" );
builder.append( (songCount > 1 ) ? " songs" : " song" );
Are they good enough with the comments above them?
I appreciate any help even if it means completely rewriting this method.