Abstraction: Writing Better Code

Abstraction: Writing Better Code

Β·

3 min read

In my last article, we got introduced to Programming and the Computer. We saw how different data items are represented in computers and why computers only understand 0s and 1s. In this article, we are going to dive into a foundational concept of programming and problem-solving in general called Abstraction.

What is Abstraction

Abstraction is simple a concept where we focus more on what an entity does rather than how it is doing it. Take for example a heater in your home, most of us just care that it heats water when we turn it on, we don't really care what happens in the internal circuitry to produce the heat.

Programmers adopted the idea of abstraction because often there are common features(functions) we use in our code and certainly, programmers before us, have already written the code for these functions and stored it somewhere. Now rather than reimplementing the logic for these functions, all we need to do is find the functions and use them, knowing they produce the output we desire. We don't really care how the function was implemented, we only care that it does what we need it to do. That's an abstraction.

Practical Example Of Abstraction

A common thing to do in programming is to output something to the screen. As a matter of fact, the famous "Hello, World" program involves printing Hello World to the screen, right?πŸ˜‰

Most programming languages have a convenient function for outputting stuff to the screen written by the various creators. JavaScript has console.log, Python has print, C has printf. Now, when we use these functions in our code, we are not interested in the logic of how the function manipulates the inner workings of the computer to get stuff that we want on the screen, all we know is, given input to these functions, we get to see that input printed on the screen.

Why should you care about Abstraction?

Now, you might be wondering, why are we talking about abstraction in the first place, I get it, there's stuff I use without knowing how they work. Okay, you don't have to attack me!πŸ˜‚

The biggest benefit of using abstraction in your code is readability. That fact that someone can at your code and quickly figure out what's going on because of the way you have named your functions. It makes it easier to work in a team and have other people work in your codebase and use functions you've created without trying to figure out how you wrote it.

Functions are a construct in general programming (available in most programming languages) where we write code that we can use in multiple places by calling the function rather than rewriting the same logic over and over.

Using Abstraction Today

Now, let's look at ways you can implement abstraction in your own code to write more readable and reusable code.

  • Writing modular functions: Writing a modular function means writing functions that do just one thing and one thing only. This makes it easy to identify what the code does and how to name the function.

  • Using meaningful function name: Using an appropriate function for a given action makes it instantly clear what the function is meant to do if the logic inside is correct. For example, we can easily say a function doubleNumber will double a number given to it as input.

Conclusion

Although abstraction is not a concept we talk about every day in programming, it is, however, something we use every day as programmers. It is something to be aware of as a programmer to make sure you write more readable and easy-to-understand code.

Thanks for reading. If you love articles like this, you can Follow Me On Twitter so you don't miss out on upcoming articles. In our next article, we'll have a look at JavaScript and what you need to know about it before using it. See you in the next one.

Β