Should a class provide public mutators for all its private fields? Announcing the arrival of...

How does the math work when buying airline miles?

Can anything be seen from the center of the Boötes void? How dark would it be?

How do I find out the mythology and history of my Fortress?

Wu formula for manifolds with boundary

Is it fair for a professor to grade us on the possession of past papers?

First console to have temporary backward compatibility

Can a new player join a group only when a new campaign starts?

How come Sam didn't become Lord of Horn Hill?

Delete nth line from bottom

Is it a good idea to use CNN to classify 1D signal?

Why are the trig functions versine, haversine, exsecant, etc, rarely used in modern mathematics?

Why wasn't DOSKEY integrated with COMMAND.COM?

How to react to hostile behavior from a senior developer?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Why are there no cargo aircraft with "flying wing" design?

An adverb for when you're not exaggerating

Do square wave exist?

Fundamental Solution of the Pell Equation

Can a party unilaterally change candidates in preparation for a General election?

Dating a Former Employee

old style "caution" boxes

Chinese Seal on silk painting - what does it mean?

Crossing US/Canada Border for less than 24 hours

Is it common practice to audition new musicians one-on-one before rehearsing with the entire band?



Should a class provide public mutators for all its private fields?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Should the methods of a class call its own getters and setters?What is the use of Association, Aggregation and Composition?“Default approach” when creating a class from scratch: getters for everything, or limited access?Public versus private inheritance when some of the parent's methods need to be exposed?Access fields of super class from derived classesMeaning of using getters and setters and Uses of parameterized Constructor.Encapsulation and Displaying InformationIs encapsulation still one of the elephants OOP stands on?Encapsulation and SRPAre groovy automatic getters and setter effectively any different to public variables?Should I use accessors or public static fields for global constants?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2















I work on refactoring an Java application based on a CAST audit. One of the criterion says that




To respect OO encapsulation concepts, private fields should always be
accessed through accessors




So far, so good. However, the audit highlights all private fields in the class that have no mutators, regardless how they are being used.



In my case, these fields are only accessed from inside the class.



I could provide private accessors for all of them, but for readability purposes, I do not think it is a good idea. Furthermore, I do not see how providing private getters for private attributes enhances OO encapsulation.



I could also provide public mutators for all fields. I have seen it done in many applications. Come to think of it, most IDEs provide a functionnality that automatically generates public mutators for all of the class attributes...



But I wonder, is it not against the very principle of encapsulation to provide public accessors for private attributes that are only meant to be used inside the class?



EDIT : my question is not about the use of accessors inside their own class. It is about the relevance of providing public accessors for all private fields in the class, regardless they are used outside the class or not.










share|improve this question









New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Possible duplicate of Should the methods of a class call its own getters and setters?

    – gnat
    20 hours ago











  • Your suspicions are correct: it's actually an anti-pattern to provide public mutators for all fields (or even most fields). Unfortunately this practice was historically required by some frameworks/libs (e.g. some persistence frameworks), but it's a historical artifact that goes against OOP practices.

    – Andres F.
    12 hours ago


















2















I work on refactoring an Java application based on a CAST audit. One of the criterion says that




To respect OO encapsulation concepts, private fields should always be
accessed through accessors




So far, so good. However, the audit highlights all private fields in the class that have no mutators, regardless how they are being used.



In my case, these fields are only accessed from inside the class.



I could provide private accessors for all of them, but for readability purposes, I do not think it is a good idea. Furthermore, I do not see how providing private getters for private attributes enhances OO encapsulation.



I could also provide public mutators for all fields. I have seen it done in many applications. Come to think of it, most IDEs provide a functionnality that automatically generates public mutators for all of the class attributes...



But I wonder, is it not against the very principle of encapsulation to provide public accessors for private attributes that are only meant to be used inside the class?



EDIT : my question is not about the use of accessors inside their own class. It is about the relevance of providing public accessors for all private fields in the class, regardless they are used outside the class or not.










share|improve this question









New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Possible duplicate of Should the methods of a class call its own getters and setters?

    – gnat
    20 hours ago











  • Your suspicions are correct: it's actually an anti-pattern to provide public mutators for all fields (or even most fields). Unfortunately this practice was historically required by some frameworks/libs (e.g. some persistence frameworks), but it's a historical artifact that goes against OOP practices.

    – Andres F.
    12 hours ago














2












2








2


1






I work on refactoring an Java application based on a CAST audit. One of the criterion says that




To respect OO encapsulation concepts, private fields should always be
accessed through accessors




So far, so good. However, the audit highlights all private fields in the class that have no mutators, regardless how they are being used.



In my case, these fields are only accessed from inside the class.



I could provide private accessors for all of them, but for readability purposes, I do not think it is a good idea. Furthermore, I do not see how providing private getters for private attributes enhances OO encapsulation.



I could also provide public mutators for all fields. I have seen it done in many applications. Come to think of it, most IDEs provide a functionnality that automatically generates public mutators for all of the class attributes...



But I wonder, is it not against the very principle of encapsulation to provide public accessors for private attributes that are only meant to be used inside the class?



EDIT : my question is not about the use of accessors inside their own class. It is about the relevance of providing public accessors for all private fields in the class, regardless they are used outside the class or not.










share|improve this question









New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I work on refactoring an Java application based on a CAST audit. One of the criterion says that




To respect OO encapsulation concepts, private fields should always be
accessed through accessors




So far, so good. However, the audit highlights all private fields in the class that have no mutators, regardless how they are being used.



In my case, these fields are only accessed from inside the class.



I could provide private accessors for all of them, but for readability purposes, I do not think it is a good idea. Furthermore, I do not see how providing private getters for private attributes enhances OO encapsulation.



I could also provide public mutators for all fields. I have seen it done in many applications. Come to think of it, most IDEs provide a functionnality that automatically generates public mutators for all of the class attributes...



But I wonder, is it not against the very principle of encapsulation to provide public accessors for private attributes that are only meant to be used inside the class?



EDIT : my question is not about the use of accessors inside their own class. It is about the relevance of providing public accessors for all private fields in the class, regardless they are used outside the class or not.







java encapsulation getters






share|improve this question









New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 15 hours ago







Pikuni













New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 20 hours ago









PikuniPikuni

265




265




New contributor




Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Pikuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    Possible duplicate of Should the methods of a class call its own getters and setters?

    – gnat
    20 hours ago











  • Your suspicions are correct: it's actually an anti-pattern to provide public mutators for all fields (or even most fields). Unfortunately this practice was historically required by some frameworks/libs (e.g. some persistence frameworks), but it's a historical artifact that goes against OOP practices.

    – Andres F.
    12 hours ago














  • 1





    Possible duplicate of Should the methods of a class call its own getters and setters?

    – gnat
    20 hours ago











  • Your suspicions are correct: it's actually an anti-pattern to provide public mutators for all fields (or even most fields). Unfortunately this practice was historically required by some frameworks/libs (e.g. some persistence frameworks), but it's a historical artifact that goes against OOP practices.

    – Andres F.
    12 hours ago








1




1





Possible duplicate of Should the methods of a class call its own getters and setters?

– gnat
20 hours ago





Possible duplicate of Should the methods of a class call its own getters and setters?

– gnat
20 hours ago













Your suspicions are correct: it's actually an anti-pattern to provide public mutators for all fields (or even most fields). Unfortunately this practice was historically required by some frameworks/libs (e.g. some persistence frameworks), but it's a historical artifact that goes against OOP practices.

– Andres F.
12 hours ago





Your suspicions are correct: it's actually an anti-pattern to provide public mutators for all fields (or even most fields). Unfortunately this practice was historically required by some frameworks/libs (e.g. some persistence frameworks), but it's a historical artifact that goes against OOP practices.

– Andres F.
12 hours ago










2 Answers
2






active

oldest

votes


















7














So I did some googling.



here is the rule you quote:




To respect OO encapsulation concepts, private fields should always be
accessed through accessors




https://www.appmarq.com/public/changeability,4576,Provide-accessors-to-Private-Fields



Its remediation:




Write a getter and setter to each private field




And a reference. Presumably for justification:




http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
JavaBeans(TM) Specification 1.01 Final Release - paragraph 8.3 Design
Patterns for Properties p 55




However, if you check the reference it says nothing of the sort. It's all about how properties are implemented.



That coupled with the awful appmarq site which prevents text selection. Lead me to believe that the entire CAST audit is basically worthless.



The idea that every private field should be backing a property is ridiculous. Presumably what they mean is: "Use properties rather than public fields" or maybe "Beware of stateful objects!"






share|improve this answer
























  • I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

    – Pikuni
    15 hours ago



















5














No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "131"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    });


    }
    });






    Pikuni is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f390498%2fshould-a-class-provide-public-mutators-for-all-its-private-fields%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









    7














    So I did some googling.



    here is the rule you quote:




    To respect OO encapsulation concepts, private fields should always be
    accessed through accessors




    https://www.appmarq.com/public/changeability,4576,Provide-accessors-to-Private-Fields



    Its remediation:




    Write a getter and setter to each private field




    And a reference. Presumably for justification:




    http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
    JavaBeans(TM) Specification 1.01 Final Release - paragraph 8.3 Design
    Patterns for Properties p 55




    However, if you check the reference it says nothing of the sort. It's all about how properties are implemented.



    That coupled with the awful appmarq site which prevents text selection. Lead me to believe that the entire CAST audit is basically worthless.



    The idea that every private field should be backing a property is ridiculous. Presumably what they mean is: "Use properties rather than public fields" or maybe "Beware of stateful objects!"






    share|improve this answer
























    • I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

      – Pikuni
      15 hours ago
















    7














    So I did some googling.



    here is the rule you quote:




    To respect OO encapsulation concepts, private fields should always be
    accessed through accessors




    https://www.appmarq.com/public/changeability,4576,Provide-accessors-to-Private-Fields



    Its remediation:




    Write a getter and setter to each private field




    And a reference. Presumably for justification:




    http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
    JavaBeans(TM) Specification 1.01 Final Release - paragraph 8.3 Design
    Patterns for Properties p 55




    However, if you check the reference it says nothing of the sort. It's all about how properties are implemented.



    That coupled with the awful appmarq site which prevents text selection. Lead me to believe that the entire CAST audit is basically worthless.



    The idea that every private field should be backing a property is ridiculous. Presumably what they mean is: "Use properties rather than public fields" or maybe "Beware of stateful objects!"






    share|improve this answer
























    • I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

      – Pikuni
      15 hours ago














    7












    7








    7







    So I did some googling.



    here is the rule you quote:




    To respect OO encapsulation concepts, private fields should always be
    accessed through accessors




    https://www.appmarq.com/public/changeability,4576,Provide-accessors-to-Private-Fields



    Its remediation:




    Write a getter and setter to each private field




    And a reference. Presumably for justification:




    http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
    JavaBeans(TM) Specification 1.01 Final Release - paragraph 8.3 Design
    Patterns for Properties p 55




    However, if you check the reference it says nothing of the sort. It's all about how properties are implemented.



    That coupled with the awful appmarq site which prevents text selection. Lead me to believe that the entire CAST audit is basically worthless.



    The idea that every private field should be backing a property is ridiculous. Presumably what they mean is: "Use properties rather than public fields" or maybe "Beware of stateful objects!"






    share|improve this answer













    So I did some googling.



    here is the rule you quote:




    To respect OO encapsulation concepts, private fields should always be
    accessed through accessors




    https://www.appmarq.com/public/changeability,4576,Provide-accessors-to-Private-Fields



    Its remediation:




    Write a getter and setter to each private field




    And a reference. Presumably for justification:




    http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
    JavaBeans(TM) Specification 1.01 Final Release - paragraph 8.3 Design
    Patterns for Properties p 55




    However, if you check the reference it says nothing of the sort. It's all about how properties are implemented.



    That coupled with the awful appmarq site which prevents text selection. Lead me to believe that the entire CAST audit is basically worthless.



    The idea that every private field should be backing a property is ridiculous. Presumably what they mean is: "Use properties rather than public fields" or maybe "Beware of stateful objects!"







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 17 hours ago









    EwanEwan

    44.2k33799




    44.2k33799













    • I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

      – Pikuni
      15 hours ago



















    • I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

      – Pikuni
      15 hours ago

















    I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

    – Pikuni
    15 hours ago





    I had not checked the reference (shame on me), but you are right, the content of Section 8.3 is not really about the violated rule. This criterion is quite unclear, because the description of the violation says Accessors are identified using the following java bean naming conventions: "set" and "get" followed by the name of the field with the first char in uppercase (case sensitive). For Boolean fields, the getter can also be named "is" followed by the name of the field. I think in the end, it mixes proper naming and relevance of the use of public accessors, which are different issues.

    – Pikuni
    15 hours ago













    5














    No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".






    share|improve this answer




























      5














      No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".






      share|improve this answer


























        5












        5








        5







        No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".






        share|improve this answer













        No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 20 hours ago









        CalethCaleth

        6,51221420




        6,51221420






















            Pikuni is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Pikuni is a new contributor. Be nice, and check out our Code of Conduct.













            Pikuni is a new contributor. Be nice, and check out our Code of Conduct.












            Pikuni is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Software Engineering Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f390498%2fshould-a-class-provide-public-mutators-for-all-its-private-fields%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







            Popular posts from this blog

            Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...

            Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

            VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...