Re:8 months of OCaml after 8 years of Haskell in production ...
by Tainnor on Wednesday December 04, 2024 @07:41AM
(#42316873)
Other languages can't help you track that a function that transitively calls 100 other functions is side-effect free, Haskell can.
> UUID::fromWords(getRandom(), getRandom(), getRandom(), getRandom())
This ignores that getRandom() is a side-effecting function (it has to be because it returns something different every time). And that's not just a "local variable". UUID.fromWords val1 val2 val3 val4 works in Haskell too, the "extra syntax" is to be able to all that in IO (i.e. with side effects). If you want it in a form that is more recognisable, you could write it as
getUuid :: IO UUID
getUuid = do
val1 <- getRandom
val2 <- getRandom
val3 <- getRandom
val4 <- getRandom
return UUID.fromWords val1 val2 val3 val4
but that's unnecessarily verbose if you know the idiom.Your cache example is similarly side effecting.
Sure, the statements in the function will have side-effects on local variables, but as long as the function isn't too long, and you can comprehend all of it at a time, that's not a problem.
Your examples of "clean code" aren't any cleaner than what can be found in any imperative language.
Using C++ (with std::optional), for reasons of familiarity.