is allowing direct access to class member variables from outside the class good practice?









up vote
0
down vote

favorite












Given the following class:



class ToggleOutput 

public:
uint32_t count;
ToggleOutput(PARAMETERS) //I've just removed stuff to reduce the code

// The code when setting things up


void Update() // public method to toggle a state

// this method will check if a time period has elapsed
// if the time period has elapsed, toggle an output
// Each time the output is toggled on then count gets incremented

count += 1;

;


Later on in the code, several instances of ToggleOutput get created



ToggleOutput outPut_1(PARAMETERS); // Again, PARAMETERS are just the stuff 
ToggleOutput outPut_2(PARAMETERS); // I've cut out for brevity.
ToggleOutput outPut_3(PARAMETERS);
ToggleOutput outPut_4(PARAMETERS);


during execution, I want to do stuff, based on the value of the class member variable, count. eg



if (outPut_1.count >= SOMEVALUE)
do_some_stuff();


I have been told that this is not acceptable. To follow the 'tenets of OOP', class methods should be impletmented to interact with class variables from outside of the class, eg the above code would need to become



if (outPut1.getCount() >= SOMEVALUE)


and the class variable count would need to be made private.



Is this true? Or is it acceptable to allow direct access to class variables if required










share|improve this question



















  • 3




    What happens if someone does outPut2.count = 1000000;?
    – aschepler
    Nov 11 at 4:24










  • Aside from the stupidity of doing such a thing (and I've certainly made similar mistakes in the past), I'm not quite certain of your point. Direct access, by inference, allows for the modification of the variable as well as evaluating its value
    – darrob
    Nov 11 at 4:35







  • 2




    I take it that means that's a bad thing. Encapsulation could prevent that mistake...
    – aschepler
    Nov 11 at 4:38






  • 2




    @darrob You must read this . Though that's tagged java but the concept is frequently common in all languages
    – P0W
    Nov 11 at 4:39










  • @aschepler Sorry, we were writing comments at the same time (I was modifying my comment).
    – darrob
    Nov 11 at 4:41














up vote
0
down vote

favorite












Given the following class:



class ToggleOutput 

public:
uint32_t count;
ToggleOutput(PARAMETERS) //I've just removed stuff to reduce the code

// The code when setting things up


void Update() // public method to toggle a state

// this method will check if a time period has elapsed
// if the time period has elapsed, toggle an output
// Each time the output is toggled on then count gets incremented

count += 1;

;


Later on in the code, several instances of ToggleOutput get created



ToggleOutput outPut_1(PARAMETERS); // Again, PARAMETERS are just the stuff 
ToggleOutput outPut_2(PARAMETERS); // I've cut out for brevity.
ToggleOutput outPut_3(PARAMETERS);
ToggleOutput outPut_4(PARAMETERS);


during execution, I want to do stuff, based on the value of the class member variable, count. eg



if (outPut_1.count >= SOMEVALUE)
do_some_stuff();


I have been told that this is not acceptable. To follow the 'tenets of OOP', class methods should be impletmented to interact with class variables from outside of the class, eg the above code would need to become



if (outPut1.getCount() >= SOMEVALUE)


and the class variable count would need to be made private.



Is this true? Or is it acceptable to allow direct access to class variables if required










share|improve this question



















  • 3




    What happens if someone does outPut2.count = 1000000;?
    – aschepler
    Nov 11 at 4:24










  • Aside from the stupidity of doing such a thing (and I've certainly made similar mistakes in the past), I'm not quite certain of your point. Direct access, by inference, allows for the modification of the variable as well as evaluating its value
    – darrob
    Nov 11 at 4:35







  • 2




    I take it that means that's a bad thing. Encapsulation could prevent that mistake...
    – aschepler
    Nov 11 at 4:38






  • 2




    @darrob You must read this . Though that's tagged java but the concept is frequently common in all languages
    – P0W
    Nov 11 at 4:39










  • @aschepler Sorry, we were writing comments at the same time (I was modifying my comment).
    – darrob
    Nov 11 at 4:41












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Given the following class:



class ToggleOutput 

public:
uint32_t count;
ToggleOutput(PARAMETERS) //I've just removed stuff to reduce the code

// The code when setting things up


void Update() // public method to toggle a state

// this method will check if a time period has elapsed
// if the time period has elapsed, toggle an output
// Each time the output is toggled on then count gets incremented

count += 1;

;


Later on in the code, several instances of ToggleOutput get created



ToggleOutput outPut_1(PARAMETERS); // Again, PARAMETERS are just the stuff 
ToggleOutput outPut_2(PARAMETERS); // I've cut out for brevity.
ToggleOutput outPut_3(PARAMETERS);
ToggleOutput outPut_4(PARAMETERS);


during execution, I want to do stuff, based on the value of the class member variable, count. eg



if (outPut_1.count >= SOMEVALUE)
do_some_stuff();


I have been told that this is not acceptable. To follow the 'tenets of OOP', class methods should be impletmented to interact with class variables from outside of the class, eg the above code would need to become



if (outPut1.getCount() >= SOMEVALUE)


and the class variable count would need to be made private.



Is this true? Or is it acceptable to allow direct access to class variables if required










share|improve this question















Given the following class:



class ToggleOutput 

public:
uint32_t count;
ToggleOutput(PARAMETERS) //I've just removed stuff to reduce the code

// The code when setting things up


void Update() // public method to toggle a state

// this method will check if a time period has elapsed
// if the time period has elapsed, toggle an output
// Each time the output is toggled on then count gets incremented

count += 1;

;


Later on in the code, several instances of ToggleOutput get created



ToggleOutput outPut_1(PARAMETERS); // Again, PARAMETERS are just the stuff 
ToggleOutput outPut_2(PARAMETERS); // I've cut out for brevity.
ToggleOutput outPut_3(PARAMETERS);
ToggleOutput outPut_4(PARAMETERS);


during execution, I want to do stuff, based on the value of the class member variable, count. eg



if (outPut_1.count >= SOMEVALUE)
do_some_stuff();


I have been told that this is not acceptable. To follow the 'tenets of OOP', class methods should be impletmented to interact with class variables from outside of the class, eg the above code would need to become



if (outPut1.getCount() >= SOMEVALUE)


and the class variable count would need to be made private.



Is this true? Or is it acceptable to allow direct access to class variables if required







c++ oop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:50

























asked Nov 11 at 4:21









darrob

1108




1108







  • 3




    What happens if someone does outPut2.count = 1000000;?
    – aschepler
    Nov 11 at 4:24










  • Aside from the stupidity of doing such a thing (and I've certainly made similar mistakes in the past), I'm not quite certain of your point. Direct access, by inference, allows for the modification of the variable as well as evaluating its value
    – darrob
    Nov 11 at 4:35







  • 2




    I take it that means that's a bad thing. Encapsulation could prevent that mistake...
    – aschepler
    Nov 11 at 4:38






  • 2




    @darrob You must read this . Though that's tagged java but the concept is frequently common in all languages
    – P0W
    Nov 11 at 4:39










  • @aschepler Sorry, we were writing comments at the same time (I was modifying my comment).
    – darrob
    Nov 11 at 4:41












  • 3




    What happens if someone does outPut2.count = 1000000;?
    – aschepler
    Nov 11 at 4:24










  • Aside from the stupidity of doing such a thing (and I've certainly made similar mistakes in the past), I'm not quite certain of your point. Direct access, by inference, allows for the modification of the variable as well as evaluating its value
    – darrob
    Nov 11 at 4:35







  • 2




    I take it that means that's a bad thing. Encapsulation could prevent that mistake...
    – aschepler
    Nov 11 at 4:38






  • 2




    @darrob You must read this . Though that's tagged java but the concept is frequently common in all languages
    – P0W
    Nov 11 at 4:39










  • @aschepler Sorry, we were writing comments at the same time (I was modifying my comment).
    – darrob
    Nov 11 at 4:41







3




3




What happens if someone does outPut2.count = 1000000;?
– aschepler
Nov 11 at 4:24




What happens if someone does outPut2.count = 1000000;?
– aschepler
Nov 11 at 4:24












Aside from the stupidity of doing such a thing (and I've certainly made similar mistakes in the past), I'm not quite certain of your point. Direct access, by inference, allows for the modification of the variable as well as evaluating its value
– darrob
Nov 11 at 4:35





Aside from the stupidity of doing such a thing (and I've certainly made similar mistakes in the past), I'm not quite certain of your point. Direct access, by inference, allows for the modification of the variable as well as evaluating its value
– darrob
Nov 11 at 4:35





2




2




I take it that means that's a bad thing. Encapsulation could prevent that mistake...
– aschepler
Nov 11 at 4:38




I take it that means that's a bad thing. Encapsulation could prevent that mistake...
– aschepler
Nov 11 at 4:38




2




2




@darrob You must read this . Though that's tagged java but the concept is frequently common in all languages
– P0W
Nov 11 at 4:39




@darrob You must read this . Though that's tagged java but the concept is frequently common in all languages
– P0W
Nov 11 at 4:39












@aschepler Sorry, we were writing comments at the same time (I was modifying my comment).
– darrob
Nov 11 at 4:41




@aschepler Sorry, we were writing comments at the same time (I was modifying my comment).
– darrob
Nov 11 at 4:41












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted











Or is it acceptable to allow direct access to class variables if required




A lot of research into good software engineering and programmer productivity indicates that it's typically good to hide the details of how something is implemented. If person A writes a class, then s/he has certain assumptions about how the class should work. If person B wants to use the class, then s/he often has different assumptions about how the class should work (especially if person A did not document the code well, or even at all, as is the case all too often). Then person B is likely to misuse the data in the class, which can break how the class methods work, and lead to errors that are difficult to debug, at least for person B.



In addition, by hiding the details of the class implementation, person A has the freedom to complete rework the implementation, perhaps removing the variable count and replacing it with something else. This can occur because person A figures out a better way to implement count, or because count was in there only as a debugging tool and is not necessary to the actual working of ToggleOutput, etc.



Programmers don't write code only for themselves. In general, they write code for other people, that will be maintained for other people. "Other people" includes you five years from now, when you look at how you implemented something and ask yourself, What on earth was I thinking? By keeping the details of the implementation hidden (including data) you have the freedom to change that, and client classes/software don't need to worry about it as long as the interface remains the same.






share|improve this answer



























    up vote
    0
    down vote













    Basically, member access is a rule you impose to the developers.



    It's something you put in place to prevent yourself or another developer using your class from modifying properties that are supposed to be managed only by the class itself and nobody else.



    It has nothing to do with security (well, not necessarily anyway), it's more a matter of semantics. If it's not supposed to be modified externally, it should be private.



    And why should you care? Well, it helps you keep your code coherent and organized, which is specially important if you are working with a development team or with code that you intent to distribute.



    And if you have to document your class, you only have to do so for stuff that is public, as far as the class user is concerned nothing else matters.






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245807%2fis-allowing-direct-access-to-class-member-variables-from-outside-the-class-good%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted











      Or is it acceptable to allow direct access to class variables if required




      A lot of research into good software engineering and programmer productivity indicates that it's typically good to hide the details of how something is implemented. If person A writes a class, then s/he has certain assumptions about how the class should work. If person B wants to use the class, then s/he often has different assumptions about how the class should work (especially if person A did not document the code well, or even at all, as is the case all too often). Then person B is likely to misuse the data in the class, which can break how the class methods work, and lead to errors that are difficult to debug, at least for person B.



      In addition, by hiding the details of the class implementation, person A has the freedom to complete rework the implementation, perhaps removing the variable count and replacing it with something else. This can occur because person A figures out a better way to implement count, or because count was in there only as a debugging tool and is not necessary to the actual working of ToggleOutput, etc.



      Programmers don't write code only for themselves. In general, they write code for other people, that will be maintained for other people. "Other people" includes you five years from now, when you look at how you implemented something and ask yourself, What on earth was I thinking? By keeping the details of the implementation hidden (including data) you have the freedom to change that, and client classes/software don't need to worry about it as long as the interface remains the same.






      share|improve this answer
























        up vote
        3
        down vote



        accepted











        Or is it acceptable to allow direct access to class variables if required




        A lot of research into good software engineering and programmer productivity indicates that it's typically good to hide the details of how something is implemented. If person A writes a class, then s/he has certain assumptions about how the class should work. If person B wants to use the class, then s/he often has different assumptions about how the class should work (especially if person A did not document the code well, or even at all, as is the case all too often). Then person B is likely to misuse the data in the class, which can break how the class methods work, and lead to errors that are difficult to debug, at least for person B.



        In addition, by hiding the details of the class implementation, person A has the freedom to complete rework the implementation, perhaps removing the variable count and replacing it with something else. This can occur because person A figures out a better way to implement count, or because count was in there only as a debugging tool and is not necessary to the actual working of ToggleOutput, etc.



        Programmers don't write code only for themselves. In general, they write code for other people, that will be maintained for other people. "Other people" includes you five years from now, when you look at how you implemented something and ask yourself, What on earth was I thinking? By keeping the details of the implementation hidden (including data) you have the freedom to change that, and client classes/software don't need to worry about it as long as the interface remains the same.






        share|improve this answer






















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted







          Or is it acceptable to allow direct access to class variables if required




          A lot of research into good software engineering and programmer productivity indicates that it's typically good to hide the details of how something is implemented. If person A writes a class, then s/he has certain assumptions about how the class should work. If person B wants to use the class, then s/he often has different assumptions about how the class should work (especially if person A did not document the code well, or even at all, as is the case all too often). Then person B is likely to misuse the data in the class, which can break how the class methods work, and lead to errors that are difficult to debug, at least for person B.



          In addition, by hiding the details of the class implementation, person A has the freedom to complete rework the implementation, perhaps removing the variable count and replacing it with something else. This can occur because person A figures out a better way to implement count, or because count was in there only as a debugging tool and is not necessary to the actual working of ToggleOutput, etc.



          Programmers don't write code only for themselves. In general, they write code for other people, that will be maintained for other people. "Other people" includes you five years from now, when you look at how you implemented something and ask yourself, What on earth was I thinking? By keeping the details of the implementation hidden (including data) you have the freedom to change that, and client classes/software don't need to worry about it as long as the interface remains the same.






          share|improve this answer













          Or is it acceptable to allow direct access to class variables if required




          A lot of research into good software engineering and programmer productivity indicates that it's typically good to hide the details of how something is implemented. If person A writes a class, then s/he has certain assumptions about how the class should work. If person B wants to use the class, then s/he often has different assumptions about how the class should work (especially if person A did not document the code well, or even at all, as is the case all too often). Then person B is likely to misuse the data in the class, which can break how the class methods work, and lead to errors that are difficult to debug, at least for person B.



          In addition, by hiding the details of the class implementation, person A has the freedom to complete rework the implementation, perhaps removing the variable count and replacing it with something else. This can occur because person A figures out a better way to implement count, or because count was in there only as a debugging tool and is not necessary to the actual working of ToggleOutput, etc.



          Programmers don't write code only for themselves. In general, they write code for other people, that will be maintained for other people. "Other people" includes you five years from now, when you look at how you implemented something and ask yourself, What on earth was I thinking? By keeping the details of the implementation hidden (including data) you have the freedom to change that, and client classes/software don't need to worry about it as long as the interface remains the same.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 4:39









          John Perry

          1,341718




          1,341718






















              up vote
              0
              down vote













              Basically, member access is a rule you impose to the developers.



              It's something you put in place to prevent yourself or another developer using your class from modifying properties that are supposed to be managed only by the class itself and nobody else.



              It has nothing to do with security (well, not necessarily anyway), it's more a matter of semantics. If it's not supposed to be modified externally, it should be private.



              And why should you care? Well, it helps you keep your code coherent and organized, which is specially important if you are working with a development team or with code that you intent to distribute.



              And if you have to document your class, you only have to do so for stuff that is public, as far as the class user is concerned nothing else matters.






              share|improve this answer
























                up vote
                0
                down vote













                Basically, member access is a rule you impose to the developers.



                It's something you put in place to prevent yourself or another developer using your class from modifying properties that are supposed to be managed only by the class itself and nobody else.



                It has nothing to do with security (well, not necessarily anyway), it's more a matter of semantics. If it's not supposed to be modified externally, it should be private.



                And why should you care? Well, it helps you keep your code coherent and organized, which is specially important if you are working with a development team or with code that you intent to distribute.



                And if you have to document your class, you only have to do so for stuff that is public, as far as the class user is concerned nothing else matters.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Basically, member access is a rule you impose to the developers.



                  It's something you put in place to prevent yourself or another developer using your class from modifying properties that are supposed to be managed only by the class itself and nobody else.



                  It has nothing to do with security (well, not necessarily anyway), it's more a matter of semantics. If it's not supposed to be modified externally, it should be private.



                  And why should you care? Well, it helps you keep your code coherent and organized, which is specially important if you are working with a development team or with code that you intent to distribute.



                  And if you have to document your class, you only have to do so for stuff that is public, as far as the class user is concerned nothing else matters.






                  share|improve this answer












                  Basically, member access is a rule you impose to the developers.



                  It's something you put in place to prevent yourself or another developer using your class from modifying properties that are supposed to be managed only by the class itself and nobody else.



                  It has nothing to do with security (well, not necessarily anyway), it's more a matter of semantics. If it's not supposed to be modified externally, it should be private.



                  And why should you care? Well, it helps you keep your code coherent and organized, which is specially important if you are working with a development team or with code that you intent to distribute.



                  And if you have to document your class, you only have to do so for stuff that is public, as far as the class user is concerned nothing else matters.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 4:57









                  Havenard

                  17.6k22646




                  17.6k22646



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245807%2fis-allowing-direct-access-to-class-member-variables-from-outside-the-class-good%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto