compdyn/partmc

Discussion: Avoid getters and setters?

cguzman95 opened this issue · 1 comments

This is an open discussion for a stardand to follow on the future C++ implementation.

I recently was discussing with an old friend, programmer for iOS applications and other stuff highly oriented on classes, about how to use the getters and setters. Surprisingly, he encouraged me to avoid the utilization of getters/setters and access the public variables instead. His principal argument was to program faster and simpler, but he also shared with me some interesting articles defending this argument:

https://www.javaworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html
https://dev.to/scottshipp/avoid-getters-and-setters-whenever-possible-c8m

He also said about using them only if they are doing some work (for example, a function like setDate(date) that converts Date to string is okay, but a function like our rxn_update_data_get_data() that only do update_data = this%update_data is not necessary and can be accessed as public following this standard) .

I prefer to follow this approach, what do you think @mattldawson?

Hi @cguzman95 - I totally agree. When I was working at a bank, coding in Java, a systems architect that joined the project said the exact same thing (after I had written a bunch of getters and setters, lol). He actually suggested designing the code to use public final member variables, which I think is similar to (but not exactly the same as) const member variables in c++. This way you can access them safely without a getter, and you have the safety of knowing that an object you're working with is not being secretively changed by some other part of the code. I think in our case, we may be able to make many class variables private and const and not need to provide access to them from outside the class as well. But, yes, I'm totally on-board with this approach.