Saturday, 28 February 2015

Class Design

***Recursion covered in last blog.

Laziness and over-complication reach an interesting nexus at the beginning of class design, where the temptation to jump straight into implementation details can be overwhelming. One has a flash of insight into how, generally, the class will operate then simply picks up the keyboard, cracks a Red Bull and pounds away lines of code like the main character in HBO's Silicon Valley. Like many things, as a beginner it is best not to imitate "professionals".

The real professional move is to begin designing your class abstractly: Focus on doc strings, use mind maps and flow charts, do some quality thinking. Treat your algorithms abstractly as well: "if the node is not a leaf, evaluate its children", "if the item is a list, flatten it", etc. The point is to think out the behaviour and relationships that objects (or functions) have, and then begin implementing; otherwise you are bound to get confused by your own implementation details, backtrack often and generally make mistakes. The real unfortunate part is the psychological investment we have in our work: Once we have created something, we are less inclined to give it up regardless of the practical consequences.

With recursive functions there is a level of "magical" trust you must develop (which I wrote about in my last blog) that your recursive calls will simply work; this is in order to prevent you from thinking out the entire trace. Class design requires less of a faith-leap, but does require more deliberation spent on what it is this object will do. When that is done, the how seems to follow more naturally. Conversely, one could spend a few minutes thinking about what kind of object a Subtract Square Game State must be like and then simply rush into the code. On line 300 they may realize that they have actually bundled half of the interface code into the game state, and then spend hours separating the two by rewriting methods and doc strings. Yuk.

So, like many things in life, the disciplined thing to do is actually the one that gives you the easiest time.

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.




Tuesday, 3 February 2015

Abstraction For A Concrete Reason

Throughout my entire Philosophy BA I wondered if my calling in life was to find some intellectual ivory tower within which to consider highly abstract matters, with a vague hope (but no requirement) that someone, somewhere would put my ideas to use. Since enrolling in the computer sciences I have learnt that this was never the case, and have actually gained insight about the process of learning itself.

I had a chemistry professor make an interesting remark the other day, regarding a highly arbitrary naming system for 3D molecular structures with a chiral centre: "Some of you students are mathematically minded, so when I tell you that something's arbitrary you sit back and relax. Then there's a bunch of you that get all touchy feely and want to know the reason for everything". In one witty sentence, this essentially summated my experiences learning maths and sciences throughout high school and even to this day.

Computer science makes use of highly abstract concepts: Recursion, inheritance, object-orientation and so forth. Even when applying these concepts in the form of computer code, one can get the sense that they are engaged in an abstract symbol-manipulation ritual alongside the likes of math, symbolic logic and other "purely intellectual" problem solving domains. That is not to say that these areas of expertise do not have their applications; much of what we consider to be integral to modern, civilized society depends on the applications that they do have. But what is interesting about the computer sciences is how quickly and intimately you are acquainted with its applications.

As one learns to code, they get the feeling that they are perpetually mere inches away from the capacity to build the next trendy app. The feedback loop involved is incredible: As soon as basic syntax is covered, you can "do something" with the code you write within weeks. Whether that be a basic game, or a simple script that sorts information, you constantly relate this to programs/devices with similar functionality that shape your life (and your pocket book).

In reality, a beginning programmer is likely years away from being able to build the next Tinder. However, I was never treated like it. I learnt to code in certification programs at Ryerson University, and despite my apparent skill level (or lack thereof) I was always spoken to by my teachers and peers as if I had a mere "few things" to learn before being able to whip up a fully functional iOS app. Eventually, my capacities caught up and I did just that by the end of the semester.

So in spite of the highly abstract, logically-detached nature of the code itself there is another side to computer programming that grounds it in the material world very beautifully, and attracts not just the abstract-logical types but the hands-on pragmatists and the creatives as well. It took me a university degree to figure this out, but I am interested in the tools of the former insofar as they serve the latter's ends. And so whether it be from the democratization of programming culture, or the quick feedback loop in learning to code, I find myself actually enjoying highly abstract, algorithmic problems for what may be the first time in my life.