Saturday, 28 February 2015

Summary of Object-oriented Programming

Object-oriented programming (OOP) is a style of programming which, as the name suggest, is based around using objects. An object is an instance of a class, which can either be a basic class defined by the programming language, or a new class created by the programmer. OOP focuses on using custom classes to write code in a more intuitive way.

Each class can have attributes and methods. An attribute is another object; it could even be the same type of object, as in trees. Having all the relevant data bundled into a single object makes it easier to work with, rather than keeping track of a group of different variables and what they refer too. A method is a kind of function associated with the class. By importing a class you can then also gain access to all the methods of that class. A method is typically something that every member of that class should be able to do: for instance any Rectangle object should be able to compute its area (assuming it had length and width attributes).

An important part of OOP is subclasses. A subclass is a specialized version of another class. It might have more attributes, more methods, or even alter the methods of the parent class. Subclasses are used when there is a conceptual link between two or more types of objects. Many times the parent class is not actually usable, but by using multiple subclasses of it, code can become simpler. If the parent class has a method, your code can call that method on an object without knowing which of the subclasses that object is (of course you have to be sure that it is one of them rather than another class or None). Classes and subclasses make code easier to understand and are useful tools for programmers.

Sunday, 8 February 2015

SLOG Week 5 - Tracing recursion

Recursion is similar to using helper functions. In both cases, you use a function to accomplish part of the main function's task. The difference is that a recursive function is its own helper function. Obviously this could cause an infinite loop, so the recursive function must be designed so that it will eventually not call itself again - that is, it can figure out the return value on its own. This is the base case.

In the examples we've worked on, this takes the form of an if-else statement, where we check to see if this is the base case (in several examples, the base case was when the input value was not a list), then return something simple - the input value, or a specific int if we are counting something.

Tracing recursion is a way to understand recursion for humans. By working out what a function does for the base case and simple cases, we know we can trust the function for these cases, allowing us to quickly work out what the function does for more complicated cases.

Sunday, 1 February 2015

SLOG week 4: thoughts about first weeks

In the first three weeks of class we studied classes and subclasses. This has not been too difficult for me; I think these concepts are logical. This past Thursday the first assignment was due, which I worked on alone.

There is something interesting I did with my implementation of the assignment. This has to do with the __init__ method of the GameView class. While writing it, I was trying to think of what attributes a GameView should have. The answer I came up with was: which game is being played. But the identity of the game is in fact another class! So my GameView class has a single attribute Game, which is capitalized because it is an attribute which represents a class. If a GameView gv is initialized with the SubtractSquareState class, for instance, calling gv.Game() will initialize a SubtractSquareState, as if SubtractSquareState() was called. Of course, there are plenty of other ways to accomplish this, but I was curious whether this would work. It did, so I left it in.

As for week 4, we have started learning about recursion. This is more tricky than the material of the first three weeks, but I think I understand it. Next week is the first test, which I am optimistic about.