[racket] Is this a good design

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Wed Feb 29 15:32:53 EST 2012

On 2/29/12 3:17 PM, Roelof Wobben wrote:
> Oke,
> Wierd idea that input control is not neccesarly. I have tried C and C++
> and they empasise to do input control.

[But you were taking an inconsistent approach: you checked in the input 
was a string, but then assumed it was non-empty.  If you're going to 
check, then check.  But the bigger issue is...]

We're talking about program design, which is independent of programming 
language.  The HtDP-based design is to state assumptions and obligations 
in the form of a contract (an agreement between a program and its users) 
then live up to that statement.

You can imagine alternative statements of the program contract and 
purpose that would lead to different code.  One is to drop the non-empty 
assumption and state the error case:

    ;; String -> String
    ;; Get first character of string if there is one, error otherwise

Then you'd want to specify the error behavior in examples (and write 
tests that tested this behavior) and make sure your code changed to 
reflect the revised class of data it consumes.

Yet another is:

    ;; Any -> String
    ;; Get first character if given a non-empty string, error otherwise

Again, the code changes (because the input data changed) and your tests 
should include examples drawn from the revised class of input.

Try both out.


Posted on the users mailing list.