Re:Good Design is Imperfect Design, Part 1: Honest Names
by crdrost on Wednesday August 04, 2021 @02:35PM
(#28064543)
The point of the article is that it doesn't have to fail gracefully.
Consider the API:
data Date = Date { getYear :: Int, getMonth :: Int, getDay :: Int }
deriving (Eq, Ord, Show)
addDays :: Date -> Int -> Date
addMonthsRounded :: Date -> Int -> Date
Someone who does
d `addMonthsRounded` 3
immediately has a contextual clue that there might be something fishy going on, and has a string they can google to get to the docs to find out that this "Rounded" business is all about "hey, the code
let y = (x `addMonthsRounded` 1) `addMonthsRounded` (-1)
in y == x
might sometimes return False because it truncates if your day doesn't fit in the given month."
Consider the API:
Someone who does immediately has a contextual clue that there might be something fishy going on, and has a string they can google to get to the docs to find out that this "Rounded" business is all about "hey, the code might sometimes return False because it truncates if your day doesn't fit in the given month."