Note that there are some explanatory texts on larger screens.

plurals
  1. POIsn't there a point where encapsulation gets ridiculous?
    primarykey
    data
    text
    <p>For my software development programming class we were supposed to make a "Feed Manager" type program for RSS feeds. Here is how I handled the implementation of FeedItems.</p> <p>Nice and simple:</p> <pre><code>struct FeedItem { string title; string description; string url; } </code></pre> <p>I got marked down for that, the "correct" example answer is as follows:</p> <pre><code>class FeedItem { public: FeedItem(string title, string description, string url); inline string getTitle() const { return this-&gt;title; } inline string getDescription() const { return this-&gt;description; } inline string getURL() const { return this-&gt;url; } inline void setTitle(string title) { this-&gt;title = title; } inline void setDescription(string description){ this-&gt;description = description; } inline void setURL(string url) { this-&gt;url = url; } private: string title; string description; string url; }; </code></pre> <p>Now to me, this seems stupid. I honestly can't believe I got marked down, when this does the exact same thing that mine does with a lot more overhead.</p> <hr> <p>It reminds me of how in C# people always do this:</p> <pre><code>public class Example { private int _myint; public int MyInt { get { return this._myint; } set { this._myint = value; } } } </code></pre> <p>I mean I <em>GET</em> why they do it, maybe later on they want to validate the data in the setter or increment it in the getter. But why don't you people just do THIS <em>UNTIL</em> that situation arises?</p> <pre><code>public class Example { public int MyInt; } </code></pre> <p>Sorry this is kind of a rant and not really a question, but the redundancy is maddening to me. Why are getters and setters so loved, when they are unneeded?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COIf you think this is ridiculous, you haven't used a library with a singleton Factory class that has one method that returns a single Manager instance with a single method that returns Mapper classes that return classes with Properties that contain strings of SQL statements. Oh, and each intermediate class has an Interface.
      singulars
    2. COIf you do it your way until it needs to be changed it is already too late. You have a member title (that is a string). But sometime down the road we need to change the implementation to a char array. Then unfortunately your implementation is stuck (everybody is already using title (and its part of the public API) and you can't get them to change so you can't change your implementation. If you hide the implementation behind a set method then how you implement the private part is not exposed and thus can be changed.
      singulars
    3. COPersonally I hate geters/setters (Its Java like and not C++ like). Why are you fiddling with the internals of the object. It does not look like it makes sense to alter these members once created (but this depends on the usage requirements). I would make all the members private, constructor to create them and see how the object is used. Adding unnesacery getters/setters is nearly as bad as having a public variable.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload