Modcraft - The community dedicated to quality WoW modding!

Featured => Noggit => Topic started by: medi4 on May 10, 2017, 04:47:29 pm

Title: Getters and setters are bad code practice?
Post by: medi4 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!
Title: Re: Getters and setters are bad code practice?
Post by: schlumpf 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.
Title: Re: Getters and setters are bad code practice?
Post by: medi4 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!