In structures (struct) is it necessary to use getters and setters or only in classes (class)?

Asked

Viewed 138 times

4

It is necessary to use setters and getters in structures to maintain good practice or is needed only in classes?

3 answers

5


It’s not needed in either. And this good practice business is a fallacy.

For example, in C++ it is not common to use this type of mechanism. There are those who use, but in specific application types and that usually would be better in another language.

There is room to do this, but C++ programmers are usually above average and know that in many cases it is not necessary. They don’t follow good practices, they understand everything how it works, and they know the requirements properly, and they don’t follow cake recipe. Every time you do something because it’s good practice, you’re already doing it wrong, even if it works. If you do it because you understand correctly and completely that it is necessary in that context, then you have a good chance of being the right one.

A structure and a class are essentially the same thing in C++, you can even use it differently, but technically there is no difference but the members' visibility standard.

C++ is a more dogmatic language, it allows you to do everything but expects you only to use what is really needed.

I often say, if you don’t understand all the implications of using a mechanism, don’t use it. The site is full of information on the subject in various languages. It works the same at all, the difference is only cultural and frequency that it is needed.

  • I do not know if it is right or wrong, but my doubt only increased when I saw the -1 there on the side, is there any source that it is not necessary to use getters and setters? Because from what I learned in college and youtube videos the correct thing is to make use of getters and setters not only for good practices, but because it is necessary for greater security.

  • 1

    The font Soy yo :D You will not get the answer you want. Well, actually you will, each one will give yours and you won’t know who to trust, which is why I said you have to learn everything to make your own decisions with foundation and trust and not rely on other people, even because at the time of making it is you who will have to decide and will have to know, has no way to escape, has to learn, no use following cake recipe.

  • 1

    There is not something universal that says what is right, and my answer says this. Did you see this in C++ or another language? Is the source you saw reliable? is someone who has worked with this for more than 30 years and handled various types of software, with various requirements and investigated the advantage of using each thing? Or is the source someone who learned in a book, blog, YT, or even in college and kept repeating without understanding why he was doing it? I know why I do and why I don’t. So I don’t tell others to always do or never do. Whoever says this is fundamentally wrong.

  • All right, I believe that this is perhaps going to be the best answer I’m going to have, just one thing that happened recently to me, I had made a class that stored points (for drawing on D3D9) and I had put the values x and y as public in the class, And for something that I can’t explain, my values were changing on their own to other values, I’m pretty sure there was no line of code exchanging their values, so I’ve been researching and met the getters and setters and added the getters and setters methods and switched to private and solved the problem.

  • I can’t help without seeing in detail. These low-level things nobody encapsulates anything. If you talk about getter/Setter ) It’s different when it makes sense to have a method that does something. Something else. work is different from being right, the worst thing that can happen to you is something works. Nothing guarantees that it will always work. If you’re right, it’ll always work.

1

The use of getters and setters is neither necessary nor obligatory in either case, but as you mentioned are good practices that help both in the question of encapsulation, and in facilitating the adequacy of the received value to the variable in which it will be stored, for example by performing calculations or ensuring that a string received will only have two digits.

  • 1

    This has nothing to do with safety. It has to do with abstraction and encapsulation.

  • Reply edited, thanks for the return Pablo Almeida

1

It is not required in either, but by default in structs, the attributes are public. This means that if you have a class and wants to access one of the attributes of one of its copies from the outside, you need to define the attribute as public or create the access and modifier ("getter" and "Setter") for it.

Browser other questions tagged

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