When to use a private constant?

Is it correct to use a private constant in the following situation:

Say I have a game with a life variable and a startLives variable. At the beginning of the game, I set the lives variable equal to the startLives variable. Here is how I usually did it:

private var lives:int = 0;
private var startingLives:int = 3;

private function startGame():void
{
   lives = startingLives;
}

(sample code - ActionScript btw)

My question is: should it be:

private var lives:int = 0;
private const STARTING_LIVES:int = 3;

private function startGame():void
{
  lives = STARTING_LIVES;
}

StartLives is unlikely to change at runtime, so should I use a constant and return to the variable if it is not constant?

UPDATE: the consensus seems to be that this is a good use of the constant, but what about the amdfan clause, what can you load the value from the configuration file?

+3
source share
3 answers

DEFAULT_STARTING_LIVES, namedLiveLives. startLives = DEFAULT_STARTING_LIVES. startLives . , .

( .)

+4

, const. , "", , . , () . (. , , .)

+6

, , , , .

, , , .

+2

Source: https://habr.com/ru/post/1698482/


All Articles