Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:25
Show Gist options
  • Save up1/e1d7eae1b81a08d18ab6 to your computer and use it in GitHub Desktop.
Save up1/e1d7eae1b81a08d18ab6 to your computer and use it in GitHub Desktop.
Defensive programming
public void addFriendToList(List<Friend> friends, Friend newFriend) {
if (friends != null && newFriend != null) {
friends.add(newFriend);
}
}
public List<Friend> findFavoriteFriends(Person person) {
List<Friend> favoriteFriends = new ArrayList<Friend>();
if (person != null) {
List<Friend> friends = person.getFriends();
if (friends != null) {
for (Friend friend : friends) {
if (friend != null) {
if (friend.isFavorite()) {
favoriteFriends.add(friend);
}
}
}
}
}
return favoriteFriends;
}
public Person newPerson(String name) {
Person person = new Person();
if( name != null ) {
person.setName(name);
}
return person;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment