Last active
August 29, 2015 14:25
-
-
Save up1/e1d7eae1b81a08d18ab6 to your computer and use it in GitHub Desktop.
Defensive programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void addFriendToList(List<Friend> friends, Friend newFriend) { | |
if (friends != null && newFriend != null) { | |
friends.add(newFriend); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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