This document outlines techniques for simplifying conditional expressions and method calls. For conditional expressions, it describes decomposing conditionals, consolidating expressions, removing duplicate code, replacing conditionals with polymorphism, and introducing null objects. For method calls, it suggests techniques like renaming methods, adding/removing parameters, separating queries from modifications, parameterizing methods, preserving whole objects, replacing parameters with methods, introducing parameter objects, removing setting methods, hiding methods, replacing constructors with factories, and replacing error codes with exceptions.
5. honestbee
Decompose Conditional
• You have a complicated conditional (if-then-else) statement.
• Extract methods from the condition, then part, and else parts.
Before
After
6. honestbee
Consolidate Conditional Expression - Ors
• You have a sequence of conditional tests with the same result.
• Combine them into a single conditional expression and extract it.
Before
After
8. honestbee
Consolidate Duplicate Conditional Fragments
• The same fragment of code is in all branches of a conditional expression.
• Move it outside of the expression.
Before
After
9. honestbee
Remove Control Flag - break
• You have a variable that is acting as a control flag for a series of boolean expressions.
• Use a break or return instead
Before
After
11. honestbee
Replace Nested Conditional with Guard Clauses
• A method has conditional behavior that does not make clear the normal path of
execution.
• Use guard clauses for all the special cases.
Before
After
13. honestbee
Replace Conditional with Polymorphism
• You have a conditional that chooses different behavior depending on the type of
an object.
• Move each leg of the conditional to an overriding method in a subclass. Make the
original method abstract.
25. honestbee
Separate Query from Modifier
• You have a method that returns a value but also changes the state of an object.
• Create two methods, one for the query and one for the modification.
There is a simple example in the book P226
26. honestbee
Parameterize Method
• Several methods do similar things but with different values contained in the method
body.
• Create one method that uses a parameter for the different values.
28. honestbee
Replace Parameterize with Explicit Methods
• You have a method that runs different code depending on the values of an enumerated parameter.
• Create a separate method for each value of the parameter
Before
After
29. honestbee
Preserve Whole Object
• You are getting several values from an object and passing these values as parameters in a method call.
• Send the whole object instead.
Before
After
30. honestbee
Replace Parameter with Method
• An object invokes a method, then passes the result as a parameter for a method.
• The receiver can also invoke this method. Remove the parameter and let the receiver invoke the method
Before
43. honestbee
Replace Constructor with Factory Method
• You want to do more than simple construction when you create an object.
• Replace the constructor with a factory method
Before
After
44. honestbee
Encapsulate Downcase
• A method returns an object that needs to be downcasted by its callers.
• Move the downcast to within the method.
• These case often appear with methods that return collection or iterator
Before
After
45. honestbee
Replace Error Code with Exception
• A method returns a special code to indicate an error.
• Throw an exception instead.
Before
After