In the above code we have comments to help use understand what's happening.
Instead of having the comment we can extract those statements into methods.
The advantage of creating a new method is that we get an opportunity to give the
method an english sounding name. The method name itself could reveal what's
happening and we won't need comment.
Let's extract the second statement into a method.
Now let's do the same to the next statement.
Now let's do the same to the last statement.
Now let's look at method sanitized_path
.
The whole method reads like a list of operations to be done. When we are looking
at the method we are not interested in finding out how the work is done. Rather
we are interested in seeing what is being done.
If we want to look at how then we can get into the implementation of any of
those methods.
This is a much better solution, where we have both what and how.
When statements are not extracted into methods then we only see what is being
done. So every time we come back to this code we have to take a pause and build
a mental model of what is being done by reading the code.