What should a `getSunrise()` function/computed pro...
# kotlinx-datetime
s
What should a
getSunrise()
function/computed property return if there is no sunrise (e.g Arctic Circle certain times of year)? Throw an error? Null? Special value?
c
It depends on your use case. Each option communicates a different meaning to the caller: • Throwing an exception: "It is the caller's responsibility to only call this function when there is a sunrise. They forgot to check, I'm throwing so they notice their code is wrong." • Returning `null`: "There is no sunrise available. You shouldn't care why, but you must be ready for the case where there is none." • Returning a sealed class/an Either: "Here is a list of all possible reasons this function fails, make sure to handle all of them so you can display an appropriate message to the user / have a different strategy depending on the case"
k
Perhaps return an Instant of the next sunrise on or after the given instant? For example, for 2023-10-01 at the North Pole, getSunrise() might return 2024-03-21-120000.
r
This isn't really a kotlinx-datetime question, but I agree the answer is going to be use-case dependent. I once built an API that returned
null
in this case, and then later wasn't able to tell the difference between no sunrise because the sun is up all day vs no sunrise because the sun is down all day.