How to Code: Simple Data - Weeks 4 and 5
This week I finished up Week 4 and 5 of How to Code: Simple Data from UBC and the class finally became a bit interesting. The earlier lessons were mind numbingly boring. The problem sets had no actual problem solving involved. Instead, they had me following simple step-by-step directions.
In week 4 I finally got to see the templates I practiced in earlier lessons show their worth. With more complicated problems, the templates imply the best way to use a given data type. I sometimes find myself wanting to revise a template to better suit a function. I would have gotten a more direct solution to my problem, but it makes it less modular and more difficult to modify. By strictly using the structure given in a template, I write better code.
The test examples force me to consider all the possible values a data type could have. Then the templates force me to deal with these cases one at a time. This is something I struggled with in earlier classes because I would constantly deal with errors from unexpected data.
The functional programming approach of Racket also encourages modular programing. In C and Python I had made a habit of writing long winding functions that brought be straight to the answer I wanted. But this made it easy to lose track of the data.
In functional programming, if a single function is too long, it is more apparent that I can divide it into smaller functions. A long function is unwieldy because it has functions nested within functions nested within functions nested within.... etc etc. I find this hard to read, so I break it into smaller chunks.
In one of the class discussions, a classmate talked about the importance of modular programming and well made data sets. Functions, he said, shouldn't take information it doesn't use. A function using a compound data type should utilize all of the information provided. Otherwise, consider redesigning the data or how the functions handles it. Programs written this way are more secure and efficient. A hacker for example, with have less resources to exploit in a function that takes less data.
When I started How to Code: Simple Data, I rolled my eyes at the redundancy of how to declare variables again. But now that I'm nearing the end of the class, I haven't even learned how to implement loops and I'm still being surprised by how flexible the template system is.
The fun part of programming is still in writing the body of a function. It can be hard to exercise enough patience to get through the exhaustive examples, tests and documentation. I am not a patient programmer (yet). But when I can get through each step in turn, it's worth see how easy it is to solve bigger problems.