3 ms·
This is nice! I can't help but wonder why everyone still names their API functions Foo_to_Bar when the reverse Bar_from_Foo is so much more readable when chain
by maskros 4y ago
This is nice!
I can't help but wonder why everyone still names their API functions Foo_to_Bar when the reverse Bar_from_Foo is so much more readable when chaining conversions.
Compare the example here:
let myoklch =
OKLab_to_OKLCH(XYZ_to_OKLab(D65_to_D50(lin_ProPhoto_to_XYZ(lin_ProPhoto(mycolor)))))
With what it could have been if it had used Bar_from_Foo naming instead:
let myoklch =
OKLCH_from_OKLab(OKLab_from_XYZ(D50_from_D65(XYZ_from_linProPhoto(lin_ProPhoto(mycolor)))))
- likium 4y agoMy take is most people write code in a write optimized manner. Foo_to_Bar is much friendlier for the writer as auto-complete will come up with all the Foo_to_Xs you can use, whereas the Bar_from_Foo format requires the writer to know that they want to use Bar up front.
- dan-robertson 4y agoIn OCaml, a common convention is often to use functions with names like int_of_float (or Int.of_float), although it is also likely that (e.g.) the Int module will have a to_float function too. I write a lot of OCaml and it still trips me up from time to time. The other solution in a procedural/functional language is to write code closer to the evaluation order, e.g. with a 'pipeline' operator: let myoklch = mycolor |> lin_proPhoto |> lin_proPhoto_to_XYZ |> ... or with a 'fluent' interface in an OOP language: let myoklch = mycolor.to_linear().to_XYZ().to_D65().to_OKLAB().to_OKLCH();