Refactoring APIs
The refactorings of chapter 11 that change the interfaces of functions and classes, with the error-handling refactorings of the online catalogue.
Techniques
- Parameterize function
Merge two or more functions that do the same work with different literal values into one function that takes the value as a parameter.
core - Preserve whole object
Pass the whole object to a function instead of several values that the caller reads out of that object first.
core - Remove flag argument
Replace an argument that a caller passes only to select what a function does with one explicit function for each choice.
core - Remove setting method
Remove the setter of a field that must not change after the object is made, and set the field once, in the constructor.
core - Replace command with function
Turn a command object that does a small job, with a constructor and one execute method, back into a plain function.
core - Replace constructor with factory function
Replace direct calls of a constructor with calls of an ordinary function that makes the object, so that the creation can have a name and can choose what to return.
core - Replace error code with exception
Replace a special return value that signals an error with a thrown exception, in languages where exceptions are the normal way to report errors.
core - Replace exception with precheck
Replace a try and catch around a call that fails in a known, expected case with a check of that case before the call.
core - Replace function with command
Turn a long or complex function into an object whose fields hold the arguments and the temporary values, and whose one method runs the work.
core - Replace parameter with query
Remove a parameter whose value the function can get by itself, for example from another parameter, and let the function compute it.
core - Replace query with parameter
Move a value that a function reads from a global or another hidden source into a parameter, so that the caller supplies it.
core - Return modified value
Change a function that updates a variable outside itself so that it computes the value and returns it, and let the caller assign the result.
core - Separate query from modifier
Split a function that both returns a value and changes state into a query that only returns the value and a modifier that only makes the change.
core