Abstraction in Action

One of the most difficult things I am facing this term is getting comfortable with the software engineering vernacular. I am taking two computer science courses and working full-time. Between school and work I hear “class instance”, “procedural programming”, and “type referencing”. These are some random examples of all the different phrases you will hear throughout your computer science career. I consistently struggle to put the correct meaning to the idea of abstraction. In Wikipedia abstraction is defined as the following: 

The process of removing physical, spatial, or temporal details or attributes in the study of objects or systems to focus attention on details of greater importance; it is similar in nature to the process of generalization [1].

Abstraction as a concept has been difficult for me to understand because it often tells you what it means but it never shows you what it’s like to implement it in real-time. Leaving me in a state of confusion because it feels like it’s an idea hiding simple code with confusing code. 

In the programming world, I will often hear “Did you initialize your class constructor?”  Or “Did you use an anonymous function?” These questions operate at different levels of abstraction. One is talking about object-oriented programming and one is talking about functions explicit without names. But in my mind, there is always a disconnect between what the question says explicitly and what it means implicitly. I always seem to miss the correct level of abstraction at which I should operate when answering these questions. I will give an example. At work, I am building an interactive activity with HTML5, BootStrap5, and vanilla JavaScript – otherwise known as ECMAScript or just “pure” JavaScript. That means I can’t use any front-end (React, Vue, Angular) or back-end (Express, Next.js, Django, Flask) frameworks. The abstractions begin to take place, should I make my activity using object-oriented techniques or should I stick to functional programming? I could just hardcode the entire activity and make no reusable code or modules. 

A Simpler Example: 

Create some objects with the following characteristics:

‘Ford’, 1990, ‘Sedan’, ‘Taurus’
‘Chevy’, 1991, ‘Pickup Truck, ‘Silverado’
‘Jeep’, 2000, ‘SUV’, ‘Wrangler’

Const object = {‘animal’: ‘dog’, ‘name’: ‘Doge’, ‘legs: 4}
Const objectOne = {Make: ‘Ford’, year: 1990, Type: ‘Sedan’, Model: ‘Taurus’};
Const objectTwo = Make: ‘Chevy, year: 1991, Type: ‘Pickup Truck, Model: ‘Silverado};
Const objectThree = {Make: ‘Jeep, year: 2000, Type: ‘SUV, Model: ‘Wrangler};

I hard coded each object and now I can use them. But if I write a function and “abstract away” the process of writing an object to a function, building objects will be easier for me if I can make a function that correctly abstracts the procedure of building objects. 

function buildObject(make, model, year, type){
	return {Make: make, Year: year, Type: type, Model: model};
}

I only have to pass the characteristics that I want in my object to my buildObject function and it will return an object. I don’t have to write the curly braces every time I want a new object. I just have to manage the work of putting in the characteristics into the function. I could use my function 10 times or 10,000 times to make a large number of objects. 

This example helps me get a firm grip on the idea of abstraction and how useful it is. This is because when we look at the question that we have to answer I can quickly see that I need to write code that can be reused any number of times. Because of this I can easily reach for a function declaration and make my objects in that manner. I could have gone another level of abstraction and created a class that builds the objects. However, I would have to operate at the object-oriented level of abstraction and would have to manipulate classes and instances to build my objects. This might be simple when the coding context is well defined, say you work at a dealership and are making their new website. You could create a class called Inventory() that holds all the objects that will be in the inventory. And the objects themselves might be classes. 

Personally, I like to go case by case and sort out the details once I have a good understanding of the scope of the project. 

[1] https://en.wikipedia.org/wiki/Abstraction_(computer_science)

Print Friendly, PDF & Email

Published by Mateo Estrada Jorge

Hi, I am a CS and physics double major. I like to rock climb, kayaking, and playing video games. I am always up for a game of chess!

Leave a comment

Your email address will not be published. Required fields are marked *