5 ms·
John Carmack may have misremembered the 10th digit of pi, but you should all search your codebases for 84,600 and see who typo'd the number of seconds in a day.
by jrockway 2y ago
John Carmack may have misremembered the 10th digit of pi, but you should all search your codebases for 84,600 and see who typo'd the number of seconds in a day. It is surprisingly common and there is a lesson about whether or not you should type constants into your program when they likely exist in the standard library of your programming language.
- TZubiri 2y agoIf you can't do multiplication and you rely on a library for that. I feel sorry for you.
- ornornor 2y agoMagic numbers something something. If your language provides it as a constant, why not use that? You’ll always get the correct value AND a descriptive name for free.
- jrockway 2y agoIt's really a matter of who's going to catch your typos. If you use the constant built into your language, millions of people are looking at it. If you type them into your throwaway bash script, nobody is going to notice you typed 60 * 60 * 24 * 356 to get a year's worth of seconds.
- nixpulvis 2y agoIf I have it I’d use it, but I’m not going to go out of my way to add another dependency for it lol.
- ClassyJacket 2y ago*the number of seconds in most days
- HPsquared 2y agoDepends what planet you're on too, I suppose.
- sambazi 2y agoearth has leap seconds
- thaumasiotes 2y agoBut those don't increase the number of seconds in the day. Instead, the final second happens twice. This is different from what happens in leap year, where February is actually lengthened and February 29 is considered a different day from February 28. It escapes me why leap seconds aren't treated that way. (I know, people say the stupid treatment of leap seconds is required by the POSIX definition of time. So what? Change the POSIX definition.)
- sambazi 2y ago> But those don't increase the number of seconds in the day. Instead, the final second happens twice. i agree that this is a rather confusing way of putting it
- leni536 2y ago> But those don't increase the number of seconds in the day. Instead, the final second happens twice No, it adds a 61th second to the last minute. UNIX timestamps happen twice, because it's broken. NTP too.
- mywittyname 2y agoI spent an embarrassing amount of time debugging why some token refresh code that was supposed to trigger every 15 minutes didn't. Eventually I had to just print out the value for each variable and found that I defined 15 minutes as 15 * 60 * 60...
- water-your-self 2y agoI like defining constants with math.
- spacebanana7 2y agoI’ve encountered similar issues for long lived tokens in cookies on Safari. Turned out my Maths was okay, but Safari under many circumstances removes first party cookies after 7 days. https://snowplow.io/blog/tracking-cookies-length/ https://snowplow.io/blog/tracking-cookies-length/
- shepherdjerred 2y agoI find it easier to do something like: SECONDS_IN_MINUTE = 60 REFRESH_TIME_MINUTES = 15 REFRESH_TIME_SECONDS = SECONDS_IN_MINUTE * REFRESH_TIME_MINUTES e.g. I define every constant separately, and ideally any constant defined by an operation will only have one such operation. This makes mistakes _much_ easier to spot.
- psd1 2y agonew TimeSpan(minutes = 15)
- shepherdjerred 2y agoDepending on the language, this is definitely preferable!
- psd1 2y agoOh the places you'll go When you roll your own date You'll think you are great 'Til you're 6 minutes slow!
- Pikamander2 2y agoOh wow, you weren't kidding. Even big projects like MySQL and Textmate look like they might have that error in their codebase. https://github.com/search?type=code&auto_enroll=true&q=84600 https://github.com/search?type=code&auto_enroll=true&q=84600 https://github.com/mysql/mysql-server/blob/824e2b4064053f7daf17d7f3f84b7a3ed92e5fb4/sql/ssl_init_callback.cc#L219 https://github.com/mysql/mysql-server/blob/824e2b4064053f7da... https://github.com/textmate/textmate/blob/346b52b108b387462d4b3def481fb74983ae89f3/Frameworks/regexp/src/format_string.cc#L220 https://github.com/textmate/textmate/blob/346b52b108b387462d...
- jrockway 2y agoThat is so smart to try it on Github. None of those look fatal but I think they all differ from the intent of the author. That's why code review is so good. You just need someone to ask "what's 874600?". Turns out: not a thing. MySQL's issue is a timeout on SSL sessions which is normally 300s. Textmate's is a "friendly" display of the time. Not a showstopper, but probably not what they had in mind. It's so easy to transpose digits. It's great to have someone ask "are you sure?".