Use Local Variables

  • Wherever reasonable, try to pick the right scope for your data
  • If data is only needed in a method call, make it local
// Using member variables
private List<Song> favoriteSongs;
public void countFavoriteSongs(List<Song> allSongs) {
    this.filterFavorites(); // Sets this.favoriteSongs
    return this.favoriteSongs.size();
}

// Use locals instead
public void countFavoriteSongs(List<Song> allSongs) {
    List<Song> favoriteSongs = this.filterFavorites(allSongs);
    return favoriteSongs.size();
}

6 / 22