N
N
nnikolaev2022-03-09 10:52:37
OOP
nnikolaev, 2022-03-09 10:52:37

Is it worth adding constants to a class?

What do you think, is it right to make definitions of constants in a class if these constants are used only inside the class. Or is it considered bad programming style?
Example:

class SomeClass {
    private readonly CONST_FOR_CLASS_ONLY = 'value of constant';

    methodOfClass() {
      // использование this.CONST_FOR_CLASS_ONLY
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2022-03-09
@nnikolaev

Constants in the class are normal practice, but they should be static, there is nothing to duplicate them in each instance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question