How to make your code more readable with abstraction

Tiago Antunes
Sep 5, 2018 · 5 min read
No one wants to feel like this!

First things first: What’s abstraction?

According to my Foundations of Programming teacher:

“Abstraction is a simplified specification of an entity”

What this means is that an abstraction is a representation of a computation entity. It is a way to conceal its particular information and only give the most relevant information to the programmer.

A Node has:
Node * left
Node * right
int val

Ok but how good is Data Abstraction?

Data abstraction allows us to transform a complex data structure into one that’s simple and easy to use. The effect of this is that a program with a high level of code complexity can be transformed into one that looks close to English (let’s call it high-level code).

  • x and y are both non-negative numbers
The implementation is different, but the output is the same
A City is made of Citizens
### OUTPUT ###
City population is 1000, random: Citizen is a female 20 years old
City population is 1000, random: Citizen is a male 74 years old
Now we’ve changed the Citizen class, really fast — and everything still works!

Wrapping up

At first abstraction might look like it’s not needed. The lower the level of the language you’re using, the more important it is to use abstraction. This avoids having complex code and really keeps it all simple. In languages like C it’s really really useful. If you doubt it, check this project I made where I implemented abstraction and it was really easy to understand what was going on.

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Tiago Antunes

Written by

Hi my name is Tiago. I’m a Computer Science student who loves programming and creating new things. I love learning about what I like, and to explain it too!

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead