No this is utterly pointless. Don't just extract literals to named constants without good reasons.
But do consider ways to explain why the a
value has to be doubled in that context. That could involve:
a function name that explains the purpose of doubling, using terms from the problem domain. For example:
function exponential_backoff(task, delay):
while task() is not success:
sleep(delay)
delay = delay * 2
a comment explaining the reason for doubling
// The backoff factor should increase the delay between retries.
// A factor of at least two guarantees that the total load stays roughly constant
// when the number of clients launching failing requests increases at a constant rate.
delay = delay * 2
extracting the factor 2
to a constant or variable if it isn't obviously and necessarily always going to be 2
.
const BACKOFF_FACTOR = 2
delay = delay * BACKOFF_FACTOR
The risk with naming variables or constants after themselves is that they might be changed in the future. For example:
const POINT_EIGHTEEN = 0.20 // actually a VAT rate but can't refactor
const TWO = 3
This is especially risky for constants that are extracted incidentally, and possibly have different meanings in different parts of the code. On the other hand, some constants are truly fixed and are not going to change, e.g. PI
, KIBIBYTE = 1024
, or SPACE = ' '
. Apply your common sense to figure out what makes sense to name, and what doesn't.
INT_TWO
makes no sense (and you have to pray that it doesn't get 'refactored' intoconst int INT_TWO = 3;
).FROBNICATION_FACTOR = 2
can make sense if that's what that 2 represents.a
is just as meaningless a name asINT_TWO