- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Had a boss once whose skills lay in communication. That is: he was really good at talking. I mean, really really good. If there was an instant of time which needed to be decorated by the sound waves caused by the operation of his vocal apparatus, that occasion was risen to with aplomb. Basically, he never stopped blethering. I mean, whatever needed discussing in the universe, he could be relied upon to express his opinion, and most of what seemed to need to be discussed was the content of his lifestyle. Erm, hang on, I seem to be channeling him at the moment, sorry, let me start again.
So, one day he decided he was going to descend into the code for some reason, so he logged into the machine, started diddling around, then decided someone needed his advice, so up he got and onto his mobile, and wandered around the building. At that point, our grandboss turned up, took at look at his screen, which he had left open, and said. "Good grief, he's coding. Don't let him code." And she expertly exited him no-save from the file he was editing, and logged him out of the system.
"Sorry," she said, "apologise to him when he comes back, and remind him I told him he's not allowed to touch code." With which she walked off.
Boss came back from his walkabout, still rabbiting away, sat back down at his machine, and carried on doing something else, unrelated to whatever he had decided to do earlier, and seemed to have completely forgotten he had wandered into the codebase.
Fortunately he was given a much-deserved promotion soon after, and was replaced by someone totally different in every way.
Admin
The simplest way I can describe what it is doing is that it returns a DateTime that is the given number of months (for a 28-day value of "month") before dtTo, or up to a week longer, while being the same day of the week and time of the day as dtFrom. Or, more mathematically, in the range (dtTo - (4month+1) weeks, dtTo - (4month) weeks] and congruent to dtFrom modulo 1 week.
Describing why it does this, however, is beyond me.
Admin
The only thing I can think of, he was trying to write some kind of scheduling, with repeating dates and what not. The requirements were, do something every N months, during the specific week M of that month.
Addendum 2022-04-11 07:32: PS. Between those requirements, and some unknown amount of mushrooms, this code was born.
Admin
I suspect the only way to work out why this does what it does, is to find out how it's used. If it's no longer used - and IME code like this often isn't - just eradicate it.
Admin
The code might try to move the start date to accomodate a fixed time period for stuff to happen. Think along the lines of "we have a due date and we always need n weeks to produce the result due at the due date". Given the due date we have to figure out when we should have started to produce the result.
Admin
This code makes no sense.
Admin
In short (as others pointed out) it does return dtTo.AddDays(-28 * month); with some rounding.
Admin
Makes me think of the 13-month calendar (https://en.wikipedia.org/wiki/International_Fixed_Calendar) I encountered many years ago while working for Kodak. Because it's rarely used, not surprising it would require custom code like that. In that calendar, if I recall correctly, "months" are fixed 4 weeks / 28 days, with some arcane rules for adding an extra week as required to keep things in alignment.
Admin
Converted it to Python out of curiosity. Doesn't seem that bad, until you give it some test data and run it... the results are maddeningly weird.
Admin
The "elsewhere" part tells me that "frighteningly" should really be "thankfully".
Admin
Thanks for that. I now understand the code. I think the biggest impediment was the
month
parameter, which I assumed represented a month e.g January, February etc.Admin
Thanks for that. I now understand the code. I think the biggest impediment was the
month
parameter, which I assumed represented a month e.g January, February etc.Admin
Well, except that because it's a more senior position, there's a good chance he will be able to do significantly more damage in the new position.
Admin
... or if you're dealing in lunar months.
Admin
Return the date x "months" before the end date, but then pick the date before that that is on the same day of the week as the "start" date. Do it very inefficiently.
Admin
I didn't totally math it out, but this might work for a very restricted number of months, say, 0-2.
But yeah, it's roughly "get the date closest to endDate that's x number of months after startDate on the same day."
Admin
A month in this case is strictly 28 days, which corresponds to the amount of time for that virus from that movie to take over the globe.
Starting with a deadline ("to" date) and a potential candidate ("from" date), see if the candidate is approximately the number of months away that you require (target). Adjust the "from" date by one month at a time until it's no more than one entire month off from the target range.
So, given an end-of-days deadline, when would you need to start, within a wave's time, to have approximately "months" number of zombie waves across the globe. This could be used, along with a survival and immunity rate, to estimate the final population on Earth after so many waves.
Admin
Actually, the function does make sense for some real-worldpurposes.
Let's say Freddy allocates charge codes for office projects only on the 13th of each month, but doesn't allocate for projects more than two two-week pay periods out. I've got something with N two-week pay periods' lead time and I need Freddy's time before that time range starts. So which date would I pick?
Admin
If your assumption is correct, the code without the 28-days magic would be like :
public DateTime GetCorrectDate(DateTime from, DateTime to, int expectedMonths) => from.AddMonth(-expectedMonths + (to-from).Months);
Admin
Maybe someone has finally discovered the way to calculate Easter or Eid?
Admin
Easter is pretty simple, just use the algorithm by Carl Friedrich Gauß developed centuries ago: https://philosophy.hi7.co/carl-friedrich-gauss--easter-5716f3f7ec9a3.html ;-)
Admin
One thought ...
Neer assume it's just bad code. Always assume it's illogical-to-insane business requirements badly coded.
Admin
Yes, its a nice one ;)
understanding it is quite another matter.
But it actually does something similar to the example here.
It calculates a base value then adjusts it back or forth depending to get the right value for the current year.
Admin
I would suggest first to comment out the function and see where you get compiler/linker errors. In many languages that will tell you all callers. Then at each call site log the caller arguments and results.
Hopefully most of the logic is never needed, and maybe much of it cannot be needed. Then we can start writing a replacement function that is well-documented and call it whenever possible. Trying to understand what the function does is probably pointless.
I once had a highly complicated function that nobody could understand and that crashed occasionally. I refactored it (refactor = no change in behaviour) and after some hours it was quite readable except two lines looking like “if (complicated expression) crash();”. I deleted these two lines and everything worked fine.
Admin
A dating site like DoULike, it often provides a user-friendly platform for singles in the USA to connect https://www.doulike.com/usa/singles.html . The site typically offers a variety of features and tools to help individuals find potential matches based on preferences, interests, and location. Users appreciate its straightforward interface and the ability to engage with others seeking similar relationships, whether for friendship, casual dating, or something more serious. As with any online platform, being mindful of safety and authenticity while interacting with others is important.
Admin
Local dating platforms like DoULike often offer a great way to connect with potential matches in your area. They focus on facilitating meaningful connections between individuals who share common interests or lifestyles. These platforms can streamline the dating process by emphasizing local connections, making it easier to meet people nearby. Features like location-based https://www.doulike.com/local-dating.html matching and personalized profiles can enhance the chances of finding compatible matches and creating genuine relationships within your community.