price as a const?
Andrew mentions that price is a good example of a variable that should be const, but prices change all the time. Please elaborate why so I can better understand. Thanks!
Joseph Wasden20,402 Points
I think this is mostly just an instructional design choice Andrew made to illustrate how one might use Const. Commerce is complicated; while in the aggregate, prices shift according with market pressures, it also isn't uncommon to find flat-rate service fees, etc. Perhaps it wasn't the best example.
One way of looking at const is, will this value need to change during the life of the program after it is assigned? Perhaps with each run of the program, we will calculate the necessary shifts in the market, and then assign it to a const variable, since once the price is calculated and assigned, we don't expect it to change.
However, do you feel you have a firm grasp of what const is? It seems you do, if you are asking questions like these. If there is still some confusion, lets hash it out!
Masha Blair13,000 Points
I agree with Joseph's explanation. And I would add that using const for price and for money values in general is especially useful so that program doesn't change the value by mistake creating a chaos in a database. If the price is changed because of a discount , for example, that would be assigned to a new variable leaving the initial price unchanged.
OK Great! Thanks again!
Libor Gess5,937 Points
this is quite confusing for me because
const or constant is something or someone that does not change as constant. The
const value you cannot change it because
TypeError: Assignment to constant variable.
on Oct 31, 2017
I would probably
use const to represent the price in my program, and assign the
const its value using some kind database query result. If the price changed, I would update the database, so the program could still work just fine regardless of price changes