Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Java and Kotlin have a nice Duration class. So in Kotlin you can do

  delay(duration = Duration.ofMinutes(minutes = 1))
which is equivalent to

  delay(timeMillis = 60_000)
Using the optional argument names here for clarity; you don't have to of course.

Sticking with the JVM, a frequent source of confusion is the epoch. Is it in millis or in seconds? With 32 bit integers that has to be seconds (which will set you up for the year 2038 problem). However, Java always used 64 bit longs for tracking the epoch in milliseconds instead even before the year 2000 problem was still a thing. Knowing which you are dealing with is kind of relevant in a lot of places; especially when interfacing with code or APIs written in other languages with more 32 bit legacy.

Things like network timeouts are usually in milliseconds. But how do you know this for sure? Specifying, them in seconds means that there's less risk of forgetting a 0 or something like that. So, using seconds is pretty common too. You can't just blindly assume one or the other. Even if you use the Duration class above, you'd still want to put the magic number it takes as a parameter in some configuration property or constant. Those need good names too and they should include the unit.



The Duration.ofMinutes thing doesn't address the exact same problem. Anyone can put the units on the "right side". You don't even need a fancy "Duration.ofMinutes" helper function. Clarity-wise that's not different than just putting a comment saying how long it is and what units. The problem in the article is getting the units on the "left side," which, yes, you have to put the units in the name of the function.


Ah no. The function accepts a `Duration` type which could be precisely 1 m 23 s 14 ms 10 us 8 ns. And so you don't need `delay_ms` vs. `delay_ns` or anything because the type encodes the precise duration. If you passed a nanosecond `Duration` into `delay` it will delay ns, if you pass a millisecond `Duration` it will delay ms.


You have to enforce the units within the function, but that’s not the same as putting them in the name of the function.

For example, in Erlang, the sleep function could/should have been written to take named tuples: `timer:sleep({seconds, 3})`


Go has time.Duration type as well and works like this:

time.Sleep(10 * time.Millisecond)


The Go compiler is also happy with time.Sleep(1000), which the new "expert full stack" dev just PR'd.

To me Durations are a zero sum gain, because in this case they hide relevant detail (int64 nanoseconds) without enforcing usage. Compare it against what Golang could easily provide -- time.SleepMilli(100), which is 40% less characters for my aging eyes to parse than time.Sleep(100 * time.Millisecond).

After all, in the very same package we have a unit in the name:

   t := time.Time{}
   t.UnixMilli() // 1647952024456




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: