Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of getters and setters for different programming languages [closed]

So I know there are a lot of questions on getters and setters in general, but I couldn't find something exactly like my question. I was wondering if people change the use of get/set depending on different languages. I started learning with C++ and was taught to use getters and setters. This is what I understand:

In C++ (and Java?), a variable can either be public or private, but we cannot have a mix. For example, I can't have a read-only variable that can still be changed inside the class. It's either all public (can read and change it), or all private (can't read and can only change inside the class). Because of this (and possibly other reasons), we use getters and setters.

In MATLAB, I can control the "setaccess" and "getaccess" properties of variables, so that I can make things read-only (can directly access the property, but can't overwrite it). In this case, I don't feel like I need a getter because I can just do class.property.

Also, in Python it is considered "Pythonic" to not use getters/setters and to only put things into properties if needed. I don't really understand why its OK to have all public variables in Python, because that's opposite of what I learned when I started with C++.

I'm just curious what other people's thoughts are on this. Would you use getters and setters for all languages? Would you only use it for C++/Java and do direct access in MATLAB and Python (which is what I am currently doing)? Is the second option considered bad? For my purposes, I am only referring to simple getters and setters (just return/set the value and do not do anything else).

Thanks!

like image 460
leonhart88 Avatar asked Jan 13 '11 18:01

leonhart88


People also ask

What is the use of Getter and setter in C++?

It sets the value for any variable which is used in the programs of a class. and starts with the word “set” followed by the variable name. Getter and Setter make the programmer convenient in setting and getting the value for a particular data type. In both getter and setter, the first letter of the variable should be capital.

How to achieve getters and setters behaviour in Python?

Using property () function to achieve getters and setters behaviour. In Python property () is a built-in function that creates and returns a property object. A property object has three methods, getter (), setter (), and delete (). property () function in Python has four arguments property (fget, fset, fdel, doc), ...

How do you reduce the number of getters and setters?

A lot of getters and setters — God object. Decouple your objects — reduce the blocks of getters and setters. Separate concerns. Reduce the number of getters and setters. A big number of accessors — tight coupling. Why are setters evil?

Do getters and setters bloat code?

Getters and setters do bloat code. There are tools to reduce getters. Lombok can generate getters and setters. Your code will have fewer lines. Getters and setters aren’t the only problem. A lot of getters and setters — God object. Decouple your objects — reduce the blocks of getters and setters. Separate concerns.


1 Answers

Actually, getters and setters (as well as public properties hiding these) are very little improvement over public variables, and a pretty good indicator for quasi classes.

like image 153
sbi Avatar answered Oct 31 '22 20:10

sbi