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.

No comments:

Post a Comment