Mod-El Code – The Every day WTF

by admin

Lengthy-lived initiatives can have… attention-grabbing little corners. Decisions made 13 years in the past can stick round, both as a result of they work effectively sufficient, or as a result of, effectively, each change breaks anyone’s workflow.

Right now’s nameless submitter was poking across the code base of a giant, long-lived JavaScript framework. In a file, not modified since 2007, however nonetheless included within the product, they discovered this operate.

_getAdjustedDay: operate(/*Date*/dateObj){ var days = [0,1,2,3,4,5,6]; if(this.weekStartsOn>0){ for(var i=0;i<this.weekStartsOn;i++){ days.unshift(days.pop()); } } return days[dateObj.getDay()]; }``` Look, that is outdated JavaScript, it is date dealing with code, and it is dealing with an uncommon date case, so we already *know* it'll be dangerous. That is not a shock in any respect. The core drawback is, given a date, we wish to discover out the day of the week it falls on, *however* weeks do not *have* to start out on Sunday, so we could must do some arithmetic to regulate the dates. That arithmetic, because the `FIXME` remark helpfully factors out, might simply be carried out with the `%` operator. *Somebody* knew the best reply right here, however did not get to implementing it. As an alternative, now we have an array of legitimate values. To calculate the offset, we "roll" the array utilizing a `unshift(pop)` combo- take the final ingredient off the array and plop it onto the entrance. We even have a bonus pointless "if" statement- the "for" loop would have dealt with that. This is not the primary time I've seen "populate an array with values and roll the array as a substitute of utilizing mod", and it most likely will not be the final. However there's additionally a bonus WTF right here. This operate is invoked in `_initFirstDay`. ```javascript _initFirstDay: operate(/*Date*/dateObj, /*Boolean*/adj){ var d = new Date(dateObj); if(!adj){d.setDate(1);} d.setDate(d.getDate()-this._getAdjustedDay(d,this.weekStartsOn)); d.setHours(0,0,0,0); return d; }

So, first off, this operate does two totally various things, relying on what you cross in for adj. Because the remark tells us, if adj is fake, we discover the primary day of the month. If adj is true, we discover the primary day of the week relative to startOfWeek. Sadly, I am unsure that remark is totally right, as a result of whether or not or not adj is fake, we do some arithmetic primarily based on _getAdjustedDay. So, for those who do that for a date in November 2020, with weeks beginning on Sunday, you get the outcomes you expect- as a result of November 1st was a Sunday. However for those who attempt it for October, the “first day” is September 27th, not October 1st.

Possibly that is by intent and design. Possibly it is not. It is exhausting to inform from the remark. However the actual bonus WTF is how they name this._getAdjustedDay here- passing in two parameters. To a operate which solely expects one. However that operate does use the worth handed in anyway, since it is a property of the category.

Even code that we are able to safely assume is dangerous simply from figuring out its origins can nonetheless discover new methods to shock us. And errors that had been most likely only a fast hack somebody deliberate to repair again in 2007 linger to this present day.

[Advertisement]
Otter – Provision your servers mechanically with out ever needing to log-in to a command immediate. Get started today!

Related Posts

Leave a Comment