ENOF (Easiest Nearest Owwie First) is a refactoring technique for working with code you don’t fully understand, named by GeePaw Hill.
Core Concept
Narrow your focus until you find something you can understand, but that takes a reading or two to grasp - it’s graspable, but not readily graspable. Fix that. Then repeat.
Three Things to Look For
1. Local Variables Named Poorly
Variables named in a way that obscures their intention.
// Before
int x = customer.GetOrders().Count();
// After - reveals intention
int orderCount = customer.GetOrders().Count();2. Declaration Without Initialization
Places where a variable is declared, but not initialized.
// Before - clue to extractable method
string message;
if (isError)
{
message = "Error occurred";
}
else
{
message = "Success";
}This is a strong clue that the if statement is an extractable method:
// After
string message = DetermineMessage(isError);
private string DetermineMessage(bool isError)
{
return isError ? "Error occurred" : "Success";
}3. Early Return Opportunities
Ways to “undent” - reduce nesting by getting out of a method with at least one case completely dealt with.
// Before - deeply nested
public void ProcessOrder(Order order)
{
if (order != null)
{
if (order.IsValid())
{
if (order.Customer.IsActive())
{
// actual logic here
}
}
}
}
// After - guard clauses
public void ProcessOrder(Order order)
{
if (order == null) return;
if (!order.IsValid()) return;
if (!order.Customer.IsActive()) return;
// actual logic here - less indented, easier to read
}Benefits of ENOF
By following this method, two things occur:
- Modest Understanding: You gain some understanding of the high-level behavior of the code
- Emergent Design: Each pass yields new results, and you start to develop a view of the big picture refactor you want to work towards
Resources
Related Concepts
- Defactoring - Inline then extract pattern
- Refactoring - Broader refactoring techniques
- Tidyings - Small, incremental improvements
- Good Software Practices