Friday, 13 February 2015

Trusting Your Technique

As a beginner, it is easy to approach algorithmic programming problems by considering every implementation detail relevant to the problem at hand. Consciously running over every line the compiler hits, keeping the changing values of variables in your own working memory, and generally tracing everything a method does in order to find out the important thing it does. However, object oriented programming teaches you, very quickly, not to do this.

One of the beauties of object oriented concepts such as public/private interfaces (which take a strange form in Python) is that you can know what a method does, without having to know how it does it. The developers behind the method were spared the luxury of being able to flip a wand and have an algorithm appear, however there is an interesting caveat to being a developer and interacting with your own code.

When you create a function, you (in some senses) become a client of your own code. You stop tracking what happens to data once it goes in and squeezes its way through the cumbersome implementation details of your method bodies. This sounds obvious, but for me it actually requires discipline to habituate. 

It is my general tendency to approach difficult matters from the maximum level of complexity: Let's consider all of the steps, possibilities, and moving parts so that we imprint as accurate a model of the problem and solution in our working memories as possible. However, past a point of bare-bones simplicity this is simply too much of a cognitive strain to be productive, or even realistic. 

Recursion was the real guru for me on this one. Given a recursive problem, it is easy (for me, at least) to fall into the trap of trying to trace each recursive call to see if function will give me the desired output. When given a simple problem to address recursively, such as sorting nodes in a tree, it can be easy to marvel at the level of intelligence your code may need: How does it know whether a node has children? How does it know how to order items in the list it returns? If there are operators in the nodes, how can it "know" to put them in the right places? Your mind goes right from the problem to the maximum level of complexity.

However, I was shown quickly that this was the cause of my troubles. As in combative sports, you have to trust your technique. In my last lab, it was repeatedly emphasized to follow the prescribed steps for solving recursive problems. The steps provided may not be the best ones for every person, but the general point stands: Without a reliable process, a beginner's mind can easily get swept away with the apparent complexity of the matter and a cognitive crash can ensue.

Address the simplest (base) case first; already you have taken care of a huge amount of the work. Now you have a foundation from which to proceed: You know what a good amount of your subsequent recursive calls will return. From then on it's a matter of following your docstring, and structuring your recursive calls so that your function returns what it says it will return. If it returns a the maximum integer in a nested list structure, return the maximum of a list of your recursive calls. Your base case and your docstring can provide you with stable ground from which to structure the intimidating "else" clause in recursive functions.

Apparently writing recursive functions gets easier, I'm looking forward to it.




1 comment:

  1. nice post. It flows like a story. It's your adventure through CSC I guess :)

    ReplyDelete