EP 10. Poorly formed problems
Sometimes, poor requirements analysis and vague problems can cause miscommunication and forces the programmer to make an assumption, which is not always good.
Take a look at this problem
Design a function that takes the image and determine whether the image is tall.
Now, this is somewhat problematic, we need clarification on what makes an image tall. Let's try to do the HtDF methodology.
The signature would be
;; Image -> Boolean
Since this seems to be a yes or no question, we are going to output a boolean.
The purpose of this function would be to produce true if the image is tall.
When designing the purpose with the boolean as the output, you must put there on what makes the condition true. It specifies on how to interpret the output.
Now we produce the stub of the function, We need to know what is the name of the function, which is not specified by the problem.
You will have to invent the name of a function, and make sure the name corresponds on the purpose of the function.
For racket, Function definitions that produce a boolean have a question mark at the end, It depends on the programming language whether question marks are allowed, as question marks are used for ternary operators in some languages.
(define (tall? img) false)
We will now write the tests
(check-expect (tall? (rectangle 2 3 "solid" "red")) true)
We will approximately 3 tests for this, will be tackled later.
Define the template
(define (tall? img)
if (... img))
We are now asking a yes or no question, we have to use an if function to make a decision.
From our intuition alone, we can determine if an image is tall if the height is greater than the width, with the guess, let us create the body
(define (tall? img)
if (> (image-height img) (image-width img))
true
false))
If you ran Dr. Racket with the code, all test would be passed, but some code is highlighted in black. It is warning us that some code is not executed by condition. When we run test cases, we should think for more scenarios, for example, what if the image is wide? To ensure that the code is correct, we need to make tests for every possible scenario.
We will add a scenario when the image is wide
(check-expect (tall? (rectangle 3 2 "solid" "red")) false)
Now all our code is evaluated. We also need to know what to do with squares, the width and height of the image is equal.
When we encounter a case where we have another condition which actually happens fairly common when we actually write the code, we need to know what to do when this happens.
We can refactor the code to accompany said condition. It can also happen to existing tests or signature..
We can assume for now that squares are not tall, nor wide
(check-expect (tall? (rectangle 3 3 "solid" "red")) false)
And we need to update the purpose on our definition of tall, which is height > width.
We can also refactor the body of the current function to make the code shorter. Since all we do anyway is to output a boolean, there are already predicates that do that.
instead of using an if function, we can use the > predicate, It produces a boolean anyway
(define (tall? img)
(> (image-height img) (image-width img)))
All tests should pass.
Mga Komento
Mag-post ng isang Komento