PowerShellPracticeAndStyle icon indicating copy to clipboard operation
PowerShellPracticeAndStyle copied to clipboard

Constant variables placement with in a script.

Open ebmarquez opened this issue 6 years ago • 1 comments
trafficstars

What is the general guidance on the use of Constance and where to place them within a script?
For example, I need to declare a URI of $api = "http://127.0.0.1:500/api/path". Is it better to place this variable next to the invoke-RestMethod or place it at the beginning of the script since it's a constant?

ebmarquez avatar Oct 23 '19 17:10 ebmarquez

I prefer at the top of the script. The purpose of constants is to be able to reuse them throughout the code, and not have paths. uri's, etc that periodically need updating being spread throughout. Leaving them at the top allows you to make them more visible to the maintainer. I always try to document them well, and that documentation could include clues to where they are used throughout the code, although that documentation in-and-of-itself could become difficult to maintain.

brianmullinsbsd avatar Oct 23 '19 17:10 brianmullinsbsd