8

When writing scripts using a modern scripting language, e.g. Powershell or JavaScript, where should I define constants? Should I make all constants global for readability and ease of use, or does it make sense to define constants as close to their scopes as possible (in a function, for instance, if it's not needed elsewhere)? I'm thinking mostly of error messages, error IDs, paths to resources or configuration options.

12

Consts are good for just giving descriptive variable names to avoid the magic number problems, but they cause a similarly annoying problem of not being able to identify the value actually being used when the const is defined away from it's use.

My suggestion, in all languages: define consts in as close a scope as possible to their actual usage. If used in only one class; only put in that class. Multiple classes would mean make sure it's at least in the same namespace as all classes using it.

In javascript this means as close lexically in scope as it can be to all usages.

  • they cause a similarly annoying problem of not being able to identify the value actually being used Shouldn't IDEs have a "go to definition" or be able to preview the value as any other var? The metadata's harder to for scripts I suppose. – StuperUser Oct 26 '12 at 15:05
  • 1
    Typically one isn't using an IDE to build shell scripts. And even when one is the dynamic nature does not lend itself to resharper style "go to definition" tooling. – Wyatt Barnett Oct 26 '12 at 15:06
  • 1
    @StuperUser I am a VS StuperUser going on 12 years now so I'm all for using the tooling available, but I think a better approach in designing is to think about how the code can be as maintainable without the tools as possible, this lends to design concepts useful across languages with less tooling, and usually has no effect on the usefulness of the tools if available. Tools at times can make bad designs easy to maintain, which is why tool reliance is often panned. – Jimmy Hoffa Oct 26 '12 at 15:09
  • similarly annoying problem of not being able to identify the value actually being used One doesn't have to check very often that GRAVITATIONAL_ACCELERATION is 9.80665. Once a constant is set you should abstract yourself for the value, and think in the name of the thing. – Tulains Córdova Oct 27 '12 at 2:34
1

Instead of using constants, you could use a configuration file. JavaScript allows you to have easily parsable files in json format. I don't know about PowerShell.

Configuration files have the advantage of being centralized, yet the name of the property in the function scope (when you retrieve it) allows you to maintain readability.

  • Sometimes that can be overkill. Especially if the value is only used internally to the code and not displayed to the user. I prefer to keep my configuration files minimal so that the app easier to setup. – David Hogue Oct 26 '12 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.