0

To provide a very blunt example (as I am at work and can't currently think of a sensible example). If I write a groovy class like this

class Wendy{
    byte[] frank

    String doSomethingWithFrank(){
       frank = someServiceThatReturnsByteArray()
    }
} 

And then groovy makes me getters and setters under the hood

how is this any different to:

class Wendy{
        public byte[] frank

        String doSomethingWithFrank(){
            frank = someServiceThatReturnsByteArray()
        }
    } 

so if you wrote

class Jenny{
    Wendy wendy = new Wendy()

    void pointless(){

       wendy.doSomethingWithFrank()
       functionThatProcessByteArray(wendy.frank)

    }

    functionThatProcessByteArray(Byte[] var1){

      // do something
    }
}

and then a month later the someServiceThatReturnsByteArray now returns a Stream

class Wendy{
        Stream susan

        String doSomethingWithFrank(){
           susan = someServiceThatReturnsByteArray // well used to
        }
    } 

groovy now automatically creates getters that return Stream rather than byte[]

the class Jenny would no longer compile as it is trying to use a byte[] as it is coded against the variable declaration not the desired user interface.

This mean that the two classes are very highly coupled, which is BAD.

I believe the point of making all variables private and controlling access is that the developer of the class can change any implementation detail inside without affecting anybody on the outside, but using this system, no detail of that variable is hidden from a user of the class.

3
  • 1
    Possible duplicate of What is the point of properties?. Note that JVM based languages have the additional facet that it's useful to have objects that can be used as Beans, and that requires getters/setters rather than public fields. – amon Jan 23 '19 at 18:17
  • @WendyG we could split your question into at least two parts. 1) Dispute of being Groovy or plain Java, standard getters and setters are no different from public properties. They are not meant for encapsulation. They are parte of the Java Bean pattern which helps standardizing things for frameworks such as Hibernate or Spring. 2) The code you showed is highly coupled because you coupled them, not because of the way getters and setters are Auto generated in Groovy. – Sidney de Moraes Jul 16 '19 at 10:54
  • Groovy works similar to Python in the matter that it assumes no kids are playing around. The developer is responsible for handling with the power of working with dynamically typed stuff, with private not being really private, etc. – Sidney de Moraes Jul 16 '19 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.