Thursday, 30 January 2014

week 4 SLOG

  This week I learned a great deal more about recursive functions, and I finally finished my first assignment, which admittedly drove me up the wall when it comes to the part where I have to code not 1, not 2, but 3 recursive functions. This amount of practice, while taxing - both mentally and physically (I couldn't sleep well because I can't stop thinking about it), led me to the realization that some problems are just impractical or plain impossible to code without using recursion. Prior to this week's class, and my work on the assignment, my opinion about recursion has not been favourable. I had always thought that recursive functions are fat, ugly functions that are not worth the effort, but now I
know better. Recursion is simple and elegant, and has the uncanny ability to reduce the length of code significantly. This is not to say that the reduction process is easy though - it is not. So, here I would like to, like last week list down a few short points for my future reference:

  • Recursion should be used when the problem cannot be, or is impractical to be solved using loops alone
  • The trick to coding a recursive function is to figure out the base case(s) - there are more often than not more than one.
  • If it is too difficult to figure out the base case(s), consider writing a more convoluted version of the function, and then reduce it.
  • Induction thinking is generally going to help.
  • Always make sure that the recursion will eventually end. 

Monday, 20 January 2014

week 3 slog



For those who are reading this post, I have a confession to make. I admit that despite having considerable prior self-taught knowledge in programming (C++) before being offered a place at the University of Toronto, I have not been a fan of object-oriented programming (OOP) until last semester, where I learnt about it for the second time in CSC108H1. Maybe it was experience, maybe it was the lack of necessity, but the programs that I wrote before U of T, I wrote every single one of them with only loops and functions - no classes, no objects. Now that I have been, should I say, reintroduced to OOP, my previous programs are starting to look very disorganised and littered with multiple redundant variables and functions.

Here, I should outline 2 of the main benefits of using OOP in programming as a reminder to myself, and for those of you who are still in doubt of the strengths of OOP:

  1. Increased Modularity - modularity has recently become a norm in most programing languages nowadays following the increasing mainstream-ness of programming itself. While one might argue that too much modularity might result in the general reduction in competence amongst programmers as a result of the oversimplification (mostly due to the increasing availability of hefty libraries online) of the process of coding itself, I personally think that the benefits still provides more than enough justification for the heavy usage of the modular approach.
  2. Simplify the Variable Creation (and Naming) Process - the concept of creating objects out of classes is one that enables us to associate certain variables or attributes to objects. The same name can be assigned to different instance variables by stringing it to its object. So, for example, a variable name like 'player_score' can be assigned to two players, each of which is an instance of a class called player, as player1.player_score and player2.player_score. This allows for a greater degree of accuracy for the coder him/herself.