4 ms·
I know that this isn't probably the case you speak of when you say "If your code reads like math, I expect that its probably done right.", but I have a huge ten
by sp4rki 14y ago
I know that this isn't probably the case you speak of when you say "If your code reads like math, I expect that its probably done right.", but I have a huge tendency to minimize code size by converting everything to "math". I converted a few hundred lines of C# code full of if statements made by a junior dev into this (more or less):
var minutesToAddToAlert = (timeToAddInMinutes) + (900 * ((timeToAddInMinutes +
(currentTimeOfDayInMinutes - 510)) / 540)) +
(2880*(((currentTimeOfDayInMinutes + (1440*dayOfTheWeek)) +
((timeToAddInMinutes) + (900 * ((timeToAddInMinutes +
(currentTimeOfDayInMinutes - 510)) / 540))) ) / 7200));
var nextAlertDate = currentDate.AddMinutes(minutesToAddToAlert);
There is definitely a lot more to it than just this, but I don't like blocks of code that don't fit on my vertical 24 inch screens. So I always end up converting "logic" (if's and else's) to math. It's a pet peeve of mine (as is overusing ternary operators as my own personal way of doing haiku), and while I certainly decrease the vertical size of the code itself, it makes it a hell of a lot harder to understand for everyone else. I then have to write comments explaining what stuff like this does, and those comments can get rather obtuse depending on the time of day.
Everyone winces when they see my commits with this message: "Math. Converted X and Y to M-A-T-H. Math. :D"
- meaty 14y agoI'd probably actually break it into TimeSpan arithmetic for the sake of clarity and the fact that it won't require comments or so many brackets: var accumulator = new TimeSpan(); accumulator.Add(TimeSpan.FromMinutes(currentTimeOfDayInMinutes)); accumulator.Add(TimeSpan.FromDays(dayOfTheWeek)); // ... etc - can't be bothered to do the rest... var nextAlertDate = currentDate.Add(accumulator); No magic numbers, no arithmetic and clear to anyone who understands the TimeSpan class (which is the all powerful lord of the fourth dimension in .Net). Oh and I work on a 1280x800 screen on an old ThinkPad T61. If the function doesn't fit on that, I'm concerned :)
- sp4rki 14y agoOh I would definitely agree with you just based on the clarity it provides (not even taking into account code size or elegance), but my coworker couldn't use TimeSpan because of certain domain specific criteria so I took his word for it and didn't even give it any thought. I just asked him what the problem/challenge was, grabbed a piece of paper, and did it with math and then just copied it to the code. In my defense, while testing both methods my solution worked reliably, while his had some pretty weird results on some extreme edge cases. With the amount of nested If/Else's he probably missed a couple of things as has been discussed on this thread previously. In my defense, moving the logic to a pure arithmetic solution resulted in a procedure that takes a bit less than one third of my screen, while the old one was at least 6 or 7 pages of scrolling through nested conditionals.