The function in question prepares data series for a plotting library so that differences (such as +10%) between the current and the previous unit of a time period can be shown (f.ex. when displaying daily totals for April, we want to see their differences from the corresponding days in March). (You have now got a little advantage of knowledge that a maintainer of the code would have to figure out herself.) The code follows; don’t be discouraged when you will feel you don’t really graps what the code does and why, I too have struggled.
A few things make the code harder to read and maintain:
- Unclear intention – no explanation of why this is done – why do we need all the current and previous series have the same number of elements and why we use an empty array for the elements that we add? Why do we replace a not-a-number (and what is it anyway, a missing data element?) with 0 and not something else?
- Unclear preconditions – no expression of the assumptions the code operates under, i.e. within which limits it is valid w.r.t. inputs and w.r.t. the use of the fake data added to the series
- Unclear inputs – lack of information about the structure of the inputs and their valid variations, which makes it hard to understand the code
- Duplication – several times we check for an array element and add it if missing; this could be generalized and the code thus shortened and simplified
- Lying comments – the initial comment at line 1 reads “add the delta to the data” – but that is not true, we are not adding the difference (e.g. +10%), we are adding the whole previous value (seriesData.previous[..] = previousValue); that’s one of disadvantges of comments – they tend to get out of sync, if they ever were correct
These are not some theoretical shortcomings. Due to them, I wasted over a day debugging our failing graphs and I still struggle to understand why the code is as is. I assume that it once worked (only rarely do people purposefully write broken code) but I can only guess at for which data and which graph types it actually worked correctly. A better structure and good comments would help me understand it faster/better and a bit of defensive programming would likely help me discover the cause hours earlier.
Here is an improved version (still far from perfect; it would be best to throw all of it away and write it properly – but that rarely works; only evolution in small steps is safe enough):
- The documentation now explains what and why we do (we may only hope that it is general enough not to get out of sync with the code anytime soon)
- We now explicitely check our assumptions about the data (assertMatchingDaysInCurrentAndPrevious) and will fail quickly if they do not hold
- Unimportant details have been moved to helper functions with helpful names so that the function’s body is shorter and much easier to read and understand
- Duplication has been removed (createDataPointIfMissing, replaceNaNWithDefaultOf)
- I’ve raised the level of abstraction in the for loop, stating clearly the intent (createDataPointIfMissing, replaceNaNWithDefaultOf, copyPreviousValueIfDefined), unobscured by the implementation details
- Bonus: As a side-effect, I have fixed a bug where we did not replace NaN in the current series, i.e. when the current series had less elements (e.g. days) than in the previous period, we added an  which then later lead to erros; we should have likely added [0, 0]
- Write your code so that the intent and implementation are separated and the intent is clear (preferably using functions with meaningful names, since comments are hard to maintain)
- Express your expectations about the inputs, either in a comment or, preferably, by actually checking them (-> defensive programming)
- Create functions on different levels of abstraction, from high-level ones stating the intent and expressing the overall flow of logic to low-level implementation details ones (and do not mix functions of different level in the same code block)
- Test your code; tests help you make sure your code works as intended and can be a great documentation of how to use it, what are valid and invalid inputs
If you do this, the maintainer of your code will love you. If you won’t, s/he will waste hours/days trying to understand what is an important part of the code and what is only an implementation detail that can be changed and trying to extract meaning and intent out of the spaghetti monster.