0

I have a doubt about if there are some recommendations for call directly an attribute in a class

I think that in OOP you always should call an attribute by the get method.

For example:

On set

public setAtribute(Atribute atr){ this.atr = atr;}

On get

 public Atribute getAtribute(){ return this.atr;}

On the constructor

public Constructor(Attribue atr){
    setAttribute(atr);
}

If the attribute is injected in the super class I use this way to call it, on inherits class:

@Inject
protected Attribute atr;

public Attribute getAttribute(Attribute atr){
    return atr;
}

I.E. I prefer to use always get and set in OOP to get access to the attributes.

But in my work there is a debate because there are people that not like this way. Because you can wrote the following code:

getAttribute().callSomeFunctionality();

But I prefer these instead of:

this.callSomeFunctionality();

And I think that this not break the Demeter´s Law because is a variable class.

What do you think about that?

Is the best use always the get and set function, or there are other cases to call directly to attribute.

There is some of the literature about this?

Thanks

10
  • 1
    Do you mean only execution WITHIN the object? If so than using 'this' is imho more natural as you don't want to encapsulate object internals from other object internals? Commented Feb 27, 2019 at 14:18
  • It depends. There's nothing that "always" is "better" no matter what in software engineering. Are we talking about plain objects (POJOs, POCOS, Mappers, etc)? Are we talking about ANY possible object in any layer?
    – Laiv
    Commented Feb 27, 2019 at 14:18
  • @Laiv I am talking in general, for example, and android project with models for get data from the api and ViewModels with MutableLiveData as an attribute, or a game with models for players and objects.
    – Tlaloc-ES
    Commented Feb 27, 2019 at 14:22
  • @kadiii Yes this is more natural, but if the parameter need some of logic in the set or in the get for example? If you put it all logic in the get or in the set you can forgot the problems and get variable in any place
    – Tlaloc-ES
    Commented Feb 27, 2019 at 14:24
  • Then, the answer is "no", "it's not always better". If you consider OOP principles like encapsulation to be anything good.
    – Laiv
    Commented Feb 27, 2019 at 14:25

1 Answer 1

6

Generally, the point of OOP is that you don't need access to the attribute.
Think about what you want to do with the attribute - whatever that is, should potentially be a method of the class, so you should call that method to do it, instead of getting the attribute value and doing it yourself.

There are exceptions, of course, but if all your 'classes' are only collections of related attributes, and the usage is coded outside of them, it's not OOP.

4
  • I think this too and for these reasons, I like to use get, but is there cases for use call to attribute like in structured programming when you are using OOP approach? is a mix not a bad option?
    – Tlaloc-ES
    Commented Feb 27, 2019 at 14:28
  • 1
    Of course, pure OOP is rarely a good idea. Commented Feb 27, 2019 at 15:31
  • 1
    Of course, pure OOP (in the Alan Kay sense) is a great idea Commented Feb 27, 2019 at 17:10
  • I think that it's far easier to read code and refactor classes when a semantically meaningful method is used over accessing properties directly. For example: if you have a LightSwitch class I want to say lightSwitch.TurnOn() instead of saying lightSwitch.onOffState = true; Commented Mar 1, 2019 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.