Asked • 07/29/19

How to properly manage "business" constants with AngularJS?

I'm working with AngularJS on a browser game and I have a bunch of "business" constants.These constants will be often updated during game testing (game balancing) and are used by different modules/features.I'm currently managing them in a single file like that:*constants.js* var gameConstants = { BASE_COST: 100, MULTIPLIER_COST: 1.07 };*myFactory.js* app.factory('MyFactory', function () { return { getRealCost: function() { return gameConstants.BASE_COST * gameConstants.MULTIPLIER_COST; } } });But I'm thinking about a service provider for game constants.Something like:*constantServices.js* app.factory('ConstantServices', function () { var baseCost: 100, //Should I use UPPER_SNAKECASE here too? multiplierCost: 1.07 return { getCost: function() { return baseCost; } getMultiplierCost: function() { return multiplierCost; } } });*myFactory.js* app.factory('MyFactory', ['ConstantServices' function (ConstantServices) { return { getRealCost: function() { return ConstantServices.getCost() * ConstantServices.getMultiplierCost(); } } });What do you think about it? Is it a good idea to create a service provider for constants only (i. e. call a service without any logic)? Do you think about a better way to manage these constants?

1 Expert Answer

By:

Matt G. answered • 02/16/23

Tutor
5 (17)

Full-Stack Software Engineer Specializing in Javascript & GraphQL

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.