<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: lsioctl</title>
    <description>The latest articles on DEV Community by lsioctl (@lsioctl).</description>
    <link>https://dev.to/lsioctl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F602301%2Ff1feea52-6e33-4f8c-b33b-f5ac3cbf70cf.jpg</url>
      <title>DEV Community: lsioctl</title>
      <link>https://dev.to/lsioctl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lsioctl"/>
    <language>en</language>
    <item>
      <title>C++ encapsulation, a tale of getters and setters ?</title>
      <dc:creator>lsioctl</dc:creator>
      <pubDate>Wed, 24 Mar 2021 18:46:34 +0000</pubDate>
      <link>https://dev.to/lsioctl/c-encapsulation-a-tale-of-getters-and-setters-3jke</link>
      <guid>https://dev.to/lsioctl/c-encapsulation-a-tale-of-getters-and-setters-3jke</guid>
      <description>&lt;h1&gt;
  
  
  Stuck in between
&lt;/h1&gt;

&lt;p&gt;On one side, there is the encapsulation mantra, which seems to be quite popular, like in Scott Meyers' books, multiple advises ... even clang-tidy has a warnings as follow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;member variable 'memberA' has public visibility
clang-tidy(cppcoreguidelines-non-private-member-variables-in-classes)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And on the other side, there is C++ pass-by/return-by value and their ghosts of performance, early optimization ...&lt;/p&gt;

&lt;p&gt;I will not argue here about any of those points, just discuss how I made my way through those two concepts that seemed to be, at first, quite opposed.&lt;/p&gt;

&lt;h1&gt;
  
  
  Struct or classes with public data members
&lt;/h1&gt;

&lt;p&gt;"A good old struct". With no real OOP mindset, it is sometime my favorite first approach, as soon as I play with everything which is not a POD (Plain Old Data) object, easily copyable.&lt;/p&gt;

&lt;p&gt;Discussing recently with a colleague, I think I finally understood when to use structs, he said something like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We use them when we group together objects that do not depend on each others&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But in any other cases ? Encapsulation to the rescue.&lt;/p&gt;

&lt;h1&gt;
  
  
  Encapsulation
&lt;/h1&gt;

&lt;p&gt;So OK, I have potentially complex objects (not PODs and certainly not easily copyable), and I make them private (farewell, clang-tidy warning). And what now ? How do I retrieve and modify them ?&lt;/p&gt;

&lt;h2&gt;
  
  
  Getters to the rescue, or ... is it ?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A getter 'by the book'
&lt;/h3&gt;

&lt;p&gt;So with my actual state of knowledge I should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;not return by value to avoid a copy, so return by reference&lt;/li&gt;
&lt;li&gt;return a const reference to avoid modifications&lt;/li&gt;
&lt;li&gt;inline for optimization&lt;/li&gt;
&lt;li&gt;noexcept for optimization (whaaat ? This I still have to read about ...)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I then create a getter which looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MyComplicatedType&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;getMemberA&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;memberA_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not bad. I am quite proud to be honest.&lt;/p&gt;

&lt;p&gt;But ...&lt;/p&gt;

&lt;p&gt;Wait, if I return a reference, how could I be sure my member could not be modified whatsoever ? Would C++ be more magical than any other languages on this matter ?&lt;/p&gt;

&lt;h3&gt;
  
  
  Safety is a const_cast away
&lt;/h3&gt;

&lt;p&gt;It is not a so advanced C++ trick to use const_cast to remove the constness, so if I  do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;constMemberA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myobject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getMemberA&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;memberA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;const_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyComplicatedType&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;constMemberA&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;memberA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;whateverIwantOfTypeMyComplicatedType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// game over&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works. I can modify my so preciously encapsulated member.&lt;/p&gt;

&lt;p&gt;Anyway, there are still advantages over a public member access and no performance issues so let's say is OK.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setter and performances
&lt;/h2&gt;

&lt;p&gt;Now it would be nice also to being able to modify my member, let's implement a setter, but before ...&lt;/p&gt;

&lt;p&gt;Thinking about it, I see no way a setter could as performant as a direct member access:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it will certainly use a copy or copy assignment constructor&lt;/li&gt;
&lt;li&gt;if it uses a move constructor, it makes the API more tricky and in a way client API will have to construct a new object&lt;/li&gt;
&lt;li&gt;there are certainly integrity checks to be done (if not what is the point of encapsulation ?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I read here and then that compilers are optimizing things, but for me it is not granted, depending on the type implementations, and maybe platform dependent (I will try to dig more those aspects).&lt;/p&gt;

&lt;p&gt;And even If I am not wanting top notch performance, I for sure do no want to add unnecessary copies at this early stage of development.&lt;/p&gt;

&lt;h1&gt;
  
  
  Encapsulation, the right way ?
&lt;/h1&gt;

&lt;p&gt;A response on a S.O post enlightened me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/13853424/getters-and-setters-is-there-performance-overhead"&gt;https://stackoverflow.com/questions/13853424/getters-and-setters-is-there-performance-overhead&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Getters and setters are signs that your class isn't designed in a useful way: if you don't make the outer behavior abstract from the internal implementation, there's no point in using an abstract interface in the first place, you might as well use a plain old struct.&lt;br&gt;
Think about what operations you really need. That's almost certainly not direct access ...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It remembered me the quote of my colleague: if we are using a class and not a struct, data members have dependencies so we may not want to let them being modified individually.&lt;/p&gt;

&lt;p&gt;I finally stepped back and asked me again: what was my class needed for ? Do I really need to own those 'complex' objects ? The answer was no and I ended up with another design, a design that encapsulates and exposes only what is needed, with no setters and getters.&lt;/p&gt;

&lt;h1&gt;
  
  
  Takeaways
&lt;/h1&gt;

&lt;p&gt;This could clearly evolve with time, but my current advice, for those undecided between the two extremes that are structs and encapsulation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if members are not dependent on each other, and no global integrity is needed, favor structs&lt;/li&gt;
&lt;li&gt;in any other cases, encapsulate and expose only methods that do no compromise the integrity of the whole class and its precious private data members&lt;/li&gt;
&lt;li&gt;encapsulation does not mean necessarily getters and setters. Both of them could come with costs like lower integrity and performance.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>oop</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
