What is abstraction and how does it influence the creation of functions?

Asked

Viewed 368 times

14

I was reading a answer about Python user’s @Cool in which he mentions the term abstraction in relation to complexity, functions and object orientation, see:

What you can do if you can’t change the function is to create abstraction, the that I always speak, you call the function you want the way you want it within a function, and there always calls this simpler function. Abstraction is a lost art that ancient programmers have always used easily to hide complexity. Here came the object orientation and people stopped thinking about it, only now they know how to create solutions complex.

This caught my attention a lot.

Doubts

  1. What in fact would be this abstraction and how can I apply it?
  2. How abstraction influences the process of creating functions?
  3. What are the main benefits of it?
  4. Object Orientation disturbs this abstraction?

If possible, could provide code examples to facilitate in the illustration.

1 answer

8


First let’s take a look at the answer that defines What is an abstraction?. Much of the question is already answered there.

What in fact would be this abstraction and how can I apply it?

The summary is to isolate something from the whole. In general it applies as a form of generalization, parameterized or not. So something that you would have to use in a complex way one or more times you put in a place and then expose just a simple way of using.

How abstraction influences the process of creating functions?

Function is an abstraction. And I’m not just talking about computation, in mathematics it’s like that. Everything you do with functions (and we do little in school because we only learn to solve easy and watertight problems to exercise the basic) you do without functions, but with functions it becomes easier to understand, it can isolate certain parts of the problem, encapsulate complexity in a fixed location and is enough DRY. You solve once a problem and no longer have to worry how it should be solved, just know how to consume the function, and although it should not be the reason, can decrease typing.

Even if you’re only going to use it once, it can be useful to give you more semantics to better name something you’re doing or to just hide the details of how the solution was given. The primary reason we use a function is always to hide the complexity of what we’re doing. The secondary is that it would avoid mistakes by having to do it again or having the same implementation in two places, which complicates maintenance.

Another reason to use abstraction is to make it flexible, so one day you can easily change an implementation without consumers knowing it happened, so it gives you flexibility to change the way you do it or even the technology used behind the scenes.

It is common, but dangerous, to create an abstraction to prevent a possible change. So one uses, for example, PDO because one day one might want to change the database. But the PDO is full of abstraction leak, so it doesn’t work so well. When the solution is complex it is common to have leaks and this is a warning sign to think if you should use this abstraction. And you never exchange database :) And the PDO is not so well implemented and you pay that price.

Already you control the pennies of values in an abstract object can be better than dealing with it manually because the amount of pennies can change or even the way you handle it can be different. Besides that there are some formulas to deal with this that are not simple and will be very used, without abstraction runs the risk of making some mistake, having to type more, not compose as well with other things (but if you do wrong can happen otherwise). Creating a data type for money is a good abstraction.

Why don’t you do it like this?

x > y ? y : x

Isn’t it simple? You’ve read this, have you noticed what’s going on right away? And if you read it like this:

Min(x, y)

Do you know what it does? It seems much easier, this function just encapsulated the expression above. Will it change one day? I doubt it. Is it very difficult to know and write that? Not so much, but you may end up missing the signal or making another mistake. Mainly it can use somewhere that mixed may seem other thing if forget parentheses, and give another result. You should think about that expression already:

(x > y ? y : x)

And depending on what was in there could generate some unwanted side effect.

Function abstraction is just that, realizing that a problem should be encapsulated in a function instead of being used directly.

You want an example of exaggeration? forEach(), she basically does this (pseudocode):

forEach(objeto, acao) {
    for (var i in objeto) acao(i);
}

forEach(objeto, (x) => print(x)); //chamando

Without abstraction it would be:

for (var i in objeto) print(i);

I put in the Github for future reference.

What are the main benefits of it?

I do not know if I can add anything here that I have not already said in the previous item. It seems to me that reducing the apparent complexity and avoiding duplications are quite obvious advantages.

Object orientation disturbs this abstraction?

OOP is a form of abstraction, and using abstraction is part of precepts of O. What I meant there is that OOP is the modinha, the people are very worried about it and do not care anymore with the basic that is to create good functions. Some will say that OOP is also creating good functions, but read material on the subject and see if any talks in detail about it. In general they are only concerned with the general structure and give silly examples.

It is not that OOP disturbs, but it is misused, as any other technique, after all everything in exaggeration is bad, the medicine turns poison. How people have been blinded to a way of doing things forget to learn the other ways and do not remember that there are simple and useful solutions without being OOP.

By the way, abstraction and encapsulation are confusing terms, but this gives a lot of story... :)

Abstraction is not and cannot make the application more complex, but many people are doing it.

How often do we not see questions here that the person can not do this basic organization?

People are so used to giving C V, following formulas, reproducing patterns, using what someone else has done, that they don’t even think it could all be more abstract. The amount of lines people write today is much greater than it should be. And the more lines in code the more complicated the code base is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.