Split phase is the technique of breaking down and separating bloated behavior. Say we have a function that has multiple behaviors or a single line of code that carries out several behaviors and it's hard to deduce the result. We would split the different phases of a program's instruction; rather than keeping them altogether to make it more understandable and readable.
In the example below, the purpose of the program is to board passengers.
We can identify four behaviors in this script:
- Parse input from user
- Separate passengers based on class
- Board the passengers in order of class
To begin refactoring, we are going to separate the behaviors into functions.
In summary we split the instructions in
rawPassengerData#map into the following functions:
#main() was added to give the program a proper starting point to call main instructions. If we were to read the instructions in
#main(), we can deduce the program will: processes a list of raw passenger data, board passengers and checks that all passengers were boarded. All without needing to know the inner workings of each function.
A good way to view the split phase technique is to keep your functions small, readable and consisting of one behavior. There is more we can do like creating classes and streamlining passenger creation. Look out for future blog posts on refactoring to see the techniques used!