Last active
August 29, 2015 14:10
-
-
Save up1/8ab1f712fd5868f10e9d to your computer and use it in GitHub Desktop.
Avoid IFs
This file contains hidden or 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
| PersonDAO personDAO = new PersonDAO(); | |
| Person person = personDAO.getPersonByID(1); | |
| if(person != null) { | |
| person.setName("My name"); | |
| } |
This file contains hidden or 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
| personDAO.getPersonByID(personID, new Action<Person>() { | |
| public void act(Person person) { | |
| person.setName("My name"); | |
| } | |
| }); |
This file contains hidden or 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 interface Action<T> { | |
| void act(T object); | |
| } |
This file contains hidden or 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 call() throws PersonNotFoundException { | |
| PersonDAO personDAO = new PersonDAO(); | |
| Person person = personDAO.getPersonByID(1); | |
| person.setName("My name"); | |
| } |
This file contains hidden or 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 String findCarInsurance(Person person) { | |
| if (person != null) { | |
| Car car = person.getCar(); | |
| if (car != null) { | |
| Insurance insurance = car.getInsurance(); | |
| if (insurance != null) { | |
| return insurance.getName(); | |
| } | |
| } | |
| } | |
| return "Unkonwn"; | |
| } |
This file contains hidden or 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 String findCarInsurance(Person person) { | |
| if (person == null) { | |
| return "Unkonwn"; | |
| } | |
| Car car = person.getCar(); | |
| if (car == null) { | |
| return "Unkonwn"; | |
| } | |
| Insurance insurance = car.getInsurance(); | |
| if (insurance == null) { | |
| return "Unkonwn"; | |
| } | |
| return insurance.getName(); | |
| } |
This file contains hidden or 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 String findCarInsurance(Optional<Person> person) { | |
| return person.flatMap(person1 -> person1.getCar()) | |
| .flatMap(car -> car.getInsurance()) | |
| .map(insurance -> insurance.getName()) | |
| .orElse("Unkonwn"); | |
| } |
This file contains hidden or 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
| List<Person> persons = personDAO.getPersonByID(1); | |
| persons.forEach(person -> { | |
| person.setName("My name"); | |
| }); |
This file contains hidden or 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
| person_dao.person(id) do |person| | |
| person.name = name | |
| end |
This file contains hidden or 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 class PersonDAO { | |
| public Person getPersonByID(int personID) throws PersonNotFoundException { | |
| Person person = null; | |
| ... | |
| ... | |
| if(person == null) { | |
| throw new PersonNotFoundException(); | |
| } | |
| return person ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment