This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: Getters and setters are bad code practice?  (Read 650 times)

medi4

  • Guest
Getters and setters are bad code practice?
« on: May 10, 2017, 04:47:29 pm »
Hi there!
in the Noggit3 contributing guidelines (https://bitbucket.org/berndloerwald/noggit3) it states that
Quote
//! \note If you really need getters and setters, your design
      //! might be broken.
What is meant by that? Isn't OOP all about the Holy and Sacred Encapsulation of data and therefore getters and setters are a necessity?
Looking forward to your arguments!

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: Getters and setters are bad code practice?
« Reply #1 on: May 10, 2017, 06:43:53 pm »
Getters and setters are the exact opposite of encapsulation: You don't hide any logic but expose everything directly. Yes, you can check things in a setter, but that's as far as encapsulation goes there. You want your classes to encapsulate _logic_. Instead of setting a state, you want to tell the class how to modify _its_ state. E.g. instead of set_x(), you want y_happened(), which implicitly updates x, hidden from the user of the class.

medi4

  • Guest
Re: Getters and setters are bad code practice?
« Reply #2 on: May 10, 2017, 08:52:56 pm »
Hmm, I guess that is why it says "might be broken" since there are still cases in which it is ok and makes sense to use getters and setters. Anyway that clears it up for me, many thanks!