Friday, January 31, 2014

Week 4: Exceptions and reflections



Four weeks have passed since the beginning of the winter term, and I have learned a lot from CSC148 in this short amount of time. Although nothing really challenged me this week, or any material up until now to be honest, I really do feel like I learned a lot from going to class, attending labs, and doing exercises. The concepts are so much easier to understand when actually applying it through labs and exercises, rather than just doing the readings.
One thing we covered on about in the past week is handling exceptions; an exception object is created whenever a runtime error occurs, it interrupt the program and print out on which line the error is found. It is very useful for debugging a program. Exceptions can also be used as pseudo if statements when using in conjunction with the try statement. With the except clause, user can raise the exception as it is generated, interrupting the program to warn the user of the error; or one can follow up the except keyword with name of the exception, as long as the error generated match the one you named, the program would continue running with the statement within the except clause without interruptions.
This technique was greatly enhanced in this week’s exercise, in which I had to raise and catch various different exceptions, including creating my own exceptions with its own customized message. I am really appreciating these exercises, especially with auto-testers to check for errors, to further improve our learning experience.

Saturday, January 25, 2014

Object-Oriented Programming



Object-Oriented Programming (OOP) gave programmers a new light in the creation of programs, proven to be extreme useful with the rising popularity of programming languages like Python, Java and C++. OOP allow us to create objects which simulate a real life object with functions and data. Objects are implemented in Python using class, including both built-in modules as well as user created ones; it has attributes in which programmers could create functions to make the object useful, and use them to interact with each other with methods such as inheritance and composition. Classes allow programmers to organize their code much better by grouping methods together, and simplify a lot of steps for large-scaled projects. Objects are used whenever they are called, and do nothing else wise, allowing users to interact with it anytime they want. Instead of affecting the entire program, any alterations can be changed directly in that specific class.

Although not too fond of the idea of programming with classes and functions in the beginning, I have really taking a liking to OOP. My first encounter with it is with Java in my grade 12 computer science class, which is new for me at the time, I wrote my first class “BankTeller” which simulate the bank, with methods such as deposit, withdraw, and check balance. At the time, I didn’t really implement a whole class, instead writing procedurally generated steps with a lot of if statements to generate a banker step by step with options; but when there’s a bug, it takes a long time to find the error, and sometimes I even had to change most of my code. With a properly written “BankTeller” class, I could have just changed the functions that caused the errors, and saved a lot of time and effort.