Register to get access to free programming courses with interactive exercises

Building abstraction using functions JS: Functions

The main reason for creating functions is to increase the abstraction level, not to reduce code duplication. The second is a consequence of the first. And indeed, instead of implementing sorting by yourself in every place where you need it, you can use the sort() function, which hides the implementation within and leaves the developer undistracted by unnecessary details (type of sorting, code to perform it).

Abstraction by functions

This is the essence of abstraction; we ignore the non-essential aspects, properties, and relationships of the object or process in question, which allows us to simplify a presentation of a complex concept. The ability to abstract is one of the most important properties of our brain. Young children learn very quickly how to classify objects in our world. Without much difficulty they understand that a tree is a tree, even if it is the first time they have seen a particular species of tree. And the two spread fingers indicate a bunny (because ears).

Despite this, an understanding of how to build abstractions wisely (in our case, distinguish functions) does not appear by itself. It comes with experience, provided there is another person stronger than you who can point out to you the mistakes you have made, and you keep track of the problems of your abstraction in the process of operation (reflection). On the other hand, excessive abstraction is more harmful than helpful. You can lose the essence and spend more time understanding what's going on behind the clutter of new entities. The construction of abstraction is always a compromise. The lack of abstraction leads to duplicate code, and the abundance can be very difficult to understand (try to grasp Category Theory) and make simple problems harder to solve.

Huge Java Call Stack

Higher-order functions take function abstraction to a new level. By delegating behavior to external code (using anonymous functions), the ability to reuse the code (algorithm) in different situations is dramatically enhanced. And instead of ten functions for ten different pieces of code, you get one function that is specified ten times with ten alternate behaviors.

But don't forget that abstractions almost always leak.

An example of a leaky abstraction

In Hexlet's first project, our students make one (they make many, but for now we are interested in one) mistake, related to the incorrect allocation of abstractions. Putting aside (abstracting!) the details, the problem boils down to writing a function that takes a number as input and has to print yes on the screen if it is even, and no otherwise.

The first solution looks something like this:

const check = (number) => {
  const result = number % 2 === 0 ? 'yes' : 'no';
  console.log(result);
};

It works, but the concept of parity itself is not isolated in its abstraction, which, for example, makes it difficult to test and understand the code. Here it is simple, but in more complex cases it is problematic to guess what kind of operation is being performed. The right thing to do would be to make parity a separate function (abstraction).

const isEven = (number) => (number % 2 === 0) ? 'yes' : 'no';

const check = (number) => {
  const result = isEven(number);
  console.log(result);
};

Look at the code above carefully. Is it okay?

This code is even worse than the first version, because the wrong abstraction is created. The number parity concept has nothing to do with the printing the "yes" or "no" strings. It exists in a vacuum as a mathematical concept and cannot know how it is going to be used. Not to mention that the name isEven() starts with is, which means that the function is a predicate. Such functions can only return a boolean value and nothing else (no exceptions!). The right version looks like this:

const isEven = (number) => (number % 2 === 0);
const check = (number) => {
  const result = isEven(number) ? 'yes' : 'no';
  console.log(result);
};

And this is the most primitive way to create abstractions. In real code, things are usually much more complicated. The construction of correct abstractions is practiced in the Hexlet projects.


Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.