Here is our array with objects identifying URL paths and additional properties
And the original iteration to identify the api/database’s value for lastScreenSaved, ex – ‘factor_affect’.
What’s wrong?? For starters, the iteration continues after identifying the correct index. Second, there are better tools to identify a substring (which we will see in the refactored code).
Let’s take a look at the refactored code:
Wow! Much more compact and clean. Allows for quicker understanding, easier testing, and faster refactoring if URL extension changes in future.
Takeaway: I tend to think in basic data structures and iterations from my early coding days. It pays to take a minute prior to merge requests to look for & refactor code to better utilize the tools available in mature languages.