[Haskell-cafe] Advice for clean code.

Don Stewart dons at galois.com
Tue Dec 4 12:07:13 EST 2007


jules:
> Felipe Lessa wrote:
> >On Dec 4, 2007 1:28 AM, Don Stewart <dons at galois.com> wrote:
> >>    -- How to display results
> >>    instance Show Action where
> >>        show MoveOutOfBounds    = "Sorry you can't move in that 
> >>        direction."
> >>        show (MoveBadTerrain a) = case a of
> >>                                  Wall      -> "You walk into a wall."
> >>                                  Tree      -> "There is a tree in the 
> >>                                  way."
> >>                                  otherwise -> "You can't move there."
> >>        show MoveOk             = "Good move."
> >
> >I always thought show was meant for returning a String that could be
> >used to recreate the original data if you copy-pasted it in your code
> >or if you used read (i.e. read . show == id). Reading the
> >documentation more carefully, I see that [1] says that this property
> >holds for *derived* instances, and says nothing about it in the
> >general case.
> 
> I would not write what dons wrote.
> 
> I would have a custom function here rather than "misusing" Show in this 
> way; call it "showMoveError" or similar. For mostly the reasons Felipe gave.

Yes, this is considered bad practice in larger project (not so much in
little hacks), since read . show should hold, as should the 'paste'
property.

-- Don


More information about the Haskell-Cafe mailing list