Introduction to Systematic Programming - Part 2
Join the DZone community and get the full member experience.Join For Free
originally authored by
late to the party? check out the earlier posts in this series:
intro to “introduction to systematic programming” course
introduction to systematic programming – week 1
week 2 of introduction to systematic programming focused primarily around how to design functions, or the htdf method. we went over the htdf recipe itself and then dove directly into 4 example problems, and discovered lots of interesting tips and tricks for working more accurately and efficiently.
lesson 1: htdf recipe
the purpose of the htdf recipe is to “systematize the design of a function” to ensure it is well tested accomplishes its stated purpose. the htdf recipe breaks down big problems into smaller, easier to manage problems, which sounds great to me. however, the instructor does make an interesting point here, stating that while this makes really big, complex problems easier to manage, the side effect is that it makes simple problems more complex.
a great tip from this lesson is to always have your design recipe “cheat sheet” with you when you’re writing your code, so that you can refer to it and make sure you’re on the right path.
lesson 2: htdf details!
after explaining the reasoning behind the htdf recipe in lesson 1, lesson 2 dives straight into explaining the different elements that make up the htdf recipe.
the signature details what type of data a function consumes and produces. for example, “number -> number” would be a signature for a function that takes multiplies 3x2.
the purpose gives a succinct description of what the function produces, given what is consumes. i can hear you currently thinking, “how is this different from the signature?!” well…. the difference is that the purpose gives context to the signature, and is intelligible to someone who may not be aware of the original spec of the problem you’re solving. an example of a purpose would be, “produce 2 times the given number” as opposed to “number -> number”.
the stub is described to be “like scaffolding”; it holds together the structure of your function so that you can run your tests, but it doesn’t actually *do* anything. it must have the correct function name, the correct number of parameters and produce a dummy result of the correct type, but again, no computations should be made in your stub. following this same example thread, an appropriate stub would be: “(define (double n) 0)”.
the example/test was briefly mentioned in the above stub section, and it is what you’d think, some small qa cases built into your program to assert that it’s working properly! both example and test are used because this portion of the recipe serves both functions. most programs will have at least two tests/examples. in our example, an appropriate test/example would be “(check-expect (double 3) 6)”.
the template is a mock function with the correct function name and the right parameter. for the rest of this week, the remainder of the template is simply (…n). following our example again, the template we would use here would be:
(define (double n)
the code is what we’ve all been waiting for and i’m sure what those of us beginners all dread, the actual function! one tip that the instructor gives here is to take a copy of the template and expand it, using the examples as models, especially for easier problems.
testing/debugging is the last stage (if we’re talking about a linear progression, that is) of the htdf recipe. this is exactly what it sounds like… run your program and hopefully your tests pass! if they don’t… find them, fix them, and try again!
additional notes from this lesson:
- type names are always capitalized!
- dr. racket checks check-expects! when you write your design recipe the first time, the test should fail because the stub does not actually do anything. the tests should not pass until you finally write your code.
lessons 3-6: htdf recipe examples
i decided to group lessons 3, 4, 5, & 6 together because in week 2, they’re all examples of how to use the htdf recipe. they’re great to watch, but to go over each example in text i don’t think would be very helpful. however! i’ve compiled a set of tips from all of these lessons so that you can check out the key points and takeaways, because i believe that this was the point of showing my classmates and myself these examples in the first place.
takeaway 1 – make sure your test is composed correctly! if your test fails after you’ve written your code, make sure you crafted the test itself properly before trying to debug your code! a small typo in a test could create hours and hours of work and frustration.
takeaway 2 – your template is just your parts list! it’s not a hard and fast rule of how you must compose your function, but it does give you the tools to use, and you can use them more than once!
takeaway 3 – don’t forget your image function requirement! (i feel like this is going to come up a lot…)
takeaway 4 – images are sized in pixels, not numbers, and pixels are always naturals. therefore when you’re calculating the size of an image, the signature should have the result as a natural, not a number.
takeaway 5 – in your signature, be the most specific. this will ensure that your program runs efficiently.
takeaway 6 – when delivering a boolean, be explicitly clear what true means, so your function knows how to react.
takeaway 7 – when crafting your tests/examples, really ask yourself how many you need. it may be 2, but it could be more! to help with this, dr. racket uses “code coverage”, which will take your tests and determine how much of your code is “covered” (or evaluated) by the tests you’ve crafted and executed. this can help you find boundary cases (also known as corner cases)
this week really emphasized, at least for me, the concept that htdf truly does make easy problems…. way more difficult. however, i do see the benefit to this design method and i’m eager to get involved in more complex problems and evaluate whether or not the htdf method really does ease the entire process.
however, what i really did appreciate was that in lesson 5, the concept of agile vs. waterfall programming was discussed, which is something that i find incredibly intriguing. before that, the htdf recipe was being explained as a very linear process, which i can appreciate for simplicity’s sake; however, everything i’ve really learned about programming so far as emphasized the agile methodology and maintaining flexibility. the instructor for the class talks about making sure that in the event of struggling to get a test to encompass a corner case or figuring out a bug your code, not to worry about going back and making changes not only to the code itself, but to the purpose or signature as well, ensuring that your program truly does cover all of the given specs and produces the most accurate results possible. love. it.
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Which Is Better for IoT: Azure RTOS or FreeRTOS?
Explainable AI: Making the Black Box Transparent
4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
What ChatGPT Needs Is Context