Being Strategic
We're going to continue working with the code we refactored in the previous sections.
By extracting more specialized and targeted methods, we were able to abstract the details down to lower levels and label our logic.
Another cause of long methods that I've seen is when you get sections of code with lengthy logic inside conditionals.
Let's take this one in particular (which has some simulated logic):
const status: OrderStatus = order.getStatus(); if (status == OrderStatus.Pending) { // Lots of logic. // Lots more logic. // Lots more logic. // Lots more logic. // Lots more logic. // Lots more logic. // Lots more logic. } else if (status == OrderStatus.Shipped) { // Lots of logic. // Lots more logic. // Lots more logic. // Lots more logic. // Lots more logic. // Lots more logic. // Lots more logic. } else if (status == OrderStatus.Cancelled) { // Lots of logic. // Lots more logic. // Lots more logic. // Lots more logic. ...