A
A
Andrey2015-08-24 23:19:27
OOP
Andrey, 2015-08-24 23:19:27

Storing a value inside a method. Good or bad?

Hello!
Suddenly I asked this question: let's say we have a base class of the following form:

class A
{
    public function isDisplayable()
    {
        return true;
    }
}

Which has, say, a method that characterizes the property of an object to be displayed.
Also, this class has several classes of heirs:
class B extends A
{
}

class C extends A
{
    public function isDisplayable()
    {
        return false;
    }
}

Duck, that's actually the question - how do you think it is appropriate to use such methods, consisting only of the return statement with the return of some value? It turns out that the object itself knows something about itself and the behavior can change from class to class. Or is it still more correct to make an external class that, taking an object of type A, will report all the necessary information about it.
I understand that the question is strongly context-dependent, but still I would like to know what everyone thinks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2015-08-24
@a_ovchinnikov

A neat example of "Replace Conditional with Polymorphism" from M. Fowler's Refactoring.
Yes, it's appropriate.

K
Ken Jee, 2015-08-24
@Machez

In general, this is normal if return returns the value of some private property. Otherwise, you can not fool around with the function, but limit yourself to the public property of the class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question