Good Practices Flashcards
1
Q
What are the 3 problems of “if” statements?
A
- Several if blocks may modify the same shared state.
- The code blocks may be duplicated.
- You have to simulate execution in your head, lik a computer would do.
2
Q
What are the alternatives to using “if” statements?
A
- Split bundled methods into new methods.
- Polymorphism: new classes extending existing ones when the intent is to add new types to an existing entity.
- NullObject or Optional type over null passing (Java only)
- Simplify an if statement tree into a single inline expression, using for example &&, ||, etc…
- Coping strategy: move the if logic further up into the call to avoid duplicating multiple times when dealing with the result.
3
Q
A