Making Life Easier with Objects

Basket of Fruits, c. 1830s, National Gallery of Art Collection

As a hobbyist, I have tried to complete at least one coding challenge a day to maintain the positive pace of learning.

Languages have now extended to Python, JavaScript, PHP, C++, C, Java, Swift, Kotlin, in order of comfort. In terms of nimbleness, I tend to think quite exclusively in Python for analytical problems, although I do use JS quite a bit when I have to, especially when I am tinkering on something in Chrome, or doing some web development.

As my coding endeavours have largely been bite-sized, I almost never had to resort to building more sophisticated architectures involving object classes (with the exception of Java which seems to be all about classes). Python and JS are such hassle-free scripting languages that I could almost get away with just some sloppy procedural programming to get a task or two done. Although I have begun to think more exclusively in functional terms after watching quite a few of Uncle Bob’s clean coding videos, even if it means my code would be more verbose with all those simple functions.

I know I should probably be planning for a chunkier, portfolio-worthy project if I am serious about coding — and perhaps I will start to work on something that could go into production.

For this month’s post, I thought for starters I could review some basic features of object-oriented programming, to compensate for the lack of air time all these months.

Let us create a very simple object called Unit_shape. This object would represent a real-life shape with a unit value for some fundamental measure, e.g. the side of a square or equilateral triangle, or even the radius of a circle.

For these three possibilities, we would ask the user to initialise the object by passing as string representing the intended shape — square, triangle or circle — into the magic method __init__ (as represented by the double underscores, or ‘dunders’, on both sides). Once the shape is known, we could proceed calculate the correct area using some knowledge in geometry learned in school.

For good measure, we also provide some description callable with the str function with the magic method __str__ in case the user would like to identify the shape of an existing Unit_shape object.

So if we test the class out by initialising an object s1 that is a unit circle, we would be able to identify both its name (more precisely string representation) and area with the code below. Notice that I did not manually calculate s1’s area — this was automatically deduced when the shape was initialised.

OOP is useful if you want to create an object that you could perform actions on or with, or you intend to have multiple objects that are similar in some way. For instance, you could define a class to represent a polynomial function, and write methods attached to the object that would enable you to plot the mathematical function, its derivative, tangent at a certain point, indefinite integral with some constant, etc.

With the class created, you could initialise a few polynomial equations and plot different graphs on each of the objects, which would be a breeze to do as compared to building them procedurally. This is probably something to explore in a future post.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store