r/PowerShell • u/ankokudaishogun • 6d ago
Question Do Constants give advantages?
So, we all know we can make a Constant by using New-Variable -Name 'ImTryingIguess" -Value 'whatever' -Option Constant
(or using Set-Variable the same way)
Question is: does using a constant instead of a regular variable give any advantage?
Like, smaller memory footprint or something?
So far, from what I managed to understand, its only effect is keeping people from altering the variable, which can be very useful with large scripts manned by multiple people, as one might decide to recycle a variable name that should otherwise stay untouched.
I personally prefix any "constant" name with CONST_(i.e.: $CONST_TitleTemplate) which, so far, has been working well, but I was wondering if I was missing some magic powershell shenanigan.
32
u/kagato87 6d ago
Yes.
As with any programming language, it makes it harder to create a regression by throwing an error when something tries to change it later on. With a regular variable you could accidentally change it, which is a pain to troubleshoot.
It's a less common issue in powershell, but the effect is the same. It makes your code easier to maintain.
4
u/uptimefordays 6d ago
No memory or perf win, a constant is the same PSVariable object with a flag set. You've got the main benefit right: protection against accidental reassignment. The catch is that `Constant` can't be removed for the life of its scope, so if you dot-source or re-run in the VS Code console you'll hit "variable is read-only or constant" until you restart the session. `ReadOnly` is usually the better tradeoff since `-Force` can clear it. For shared config, a module-scoped variable or a settings object is often cleaner than a pile of `CONST_ globals`.
5
u/DrSinistar 6d ago
Constants have never been useful for me in PowerShell.
2
u/KevMar Community Blogger 6d ago
If you break it up into multiple function and pass those values in as Parameters then this doesn't really happen so much. If they forget about it and reuse a variable name within the function, a new variable in local scope is created that doesn't change the original.
It's rare that I ever have a reason to call
Set-Variable. It's usually a symptom of something else.1
u/NorCalFrances 6d ago
Only for code safety & ensuring the value / name doesn't accidentally get reused. Constants are treated by the PS interpreter as a variable with an extra flag, so technically there's a tiny bit of added overhead.
1
u/surfingoldelephant 6d ago edited 6d ago
Not really. They're regular
PSVariables withConstantset as theScopedItemOptions. It's only enforced at runtime, meaning there's no protection during parse time. Also, because of PowerShell's dynamic scoping, it's still easy to unintentionally shadow aConstant/ReadOnlyvariable (unless it's also created withAllScope, but even that doesn't cover all scenarios). It can make using the debugger harder as well if you need to modify something on the fly.There's also a bug in Windows PowerShell that allows them to be modified with type-constraining (fixed in v7):
In their current state, personally I think
Constant/ReadOnlyoptions are best left to automatic variables. I'd only really consider using them for my own variables if they were reworked to be part of parse time.1
u/OwwwnShip 5d ago
If the value is one of a fixed set, an enum beats both. `enum Env { Dev; Stage; Prod }` with a `[Env]$Target` parameter rejects a typo at bind time instead of silently taking whatever string came in, and it reads cleaner than $CONST_TargetEnv.
For loose values I stopped bothering with constants a while back. A fresh scope per function does the same job with less swearing at "variable is read-only" when you re-run something in the same session.