Index

Overview


Simplifying a pain point. This is probably the best reason for adding a helper method. The reason is that we aim to keep our methods short, clear and understandable. Especially important when coders are browsing the code a month, year, ... later.

The Pain Point


Let's say we have a problem where we are coding a tuple (a member of the collection/list family).

We need to code methods like get and slice that deal with indexes. That should be trivial right, assuming our internal data is a list or array, we just delegate to our data.

Oops, not so fast, we see that our tuple requirements allow for negative or null indexes. These will need to be transformed into "simple" indexes! 😕

So something that should be as trivial as:

internalList.get(simpleIndex)


becomes much more complex. 😢

The Simplification


We can use a pain point such as this as an indicator of the need for a helper method.

If we move the code for the pain point off into a helper method than the original method will be as simple (trivial) as we originally expected! 😊

For our example problem we might want to add helper methods such as "conformIndex" and "conformLastIndex" that receive the "complex" index and return a "simple" index. In other words, the method parameter may be positive, negative or null. But the conformed/converted index will be the familiar simple index (zero to size minus one).

Two winners. The helper methods, because isolated, are not too complex, and thus we win there. And the big winner is the calling (original) method that now only needs to think in terms of simple indexes.