Suggestion: Improve syntax of "if not exist"
See original GitHub issueRelated StackOverflow Q/A: http://stackoverflow.com/a/31888581/450913
The syntax of Test-Path
in conditional statements is unnecessarily verbose (specially in the negated case), and prone to logical errors if parenthesis are not used properly:
if (Test-Path $path) { ... }
if ($path | Test-Path) { ... }
if (-not (Test-Path $path)) { ... }
if (-not ($path | Test-Path)) { ... }
if (!(Test-Path $path)) { ... }
if (!($path | Test-Path)) { ... }
I would like to suggest the following aliases to be supported natively in PowerShell:
function not-exist { -not (Test-Path $args) }
Set-Alias !exist not-exist -Option "Constant, AllScope"
Set-Alias exist Test-Path -Option "Constant, AllScope"
With that, the conditional statements will change to:
if (exist $path) { ... }
and
if (not-exist $path) { ... }
if (!exist $path) { ... }
Much more readable and avoids logical errors that happen in statements such as: if (-not $path | Test-Path) { ... }
Another idea is to implement them natively as “-Exist” and “-NotExist” operators.
Also as suggested by Derp McDerp in UserVoice item:
An alternative way of fixing this is for powershell to alter the grammar to allow the following syntax sugar:
if !(expr) { statements* } if -not (expr) { statements* }
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:14 (2 by maintainers)
Top Results From Across the Web
[Suggestion] Loaded add-on conditional syntax · Issue #1449
I was thinking it would be cool, if there was a condition, that could tell if a certain add-on was loaded, and if...
Read more >SQL Server 2008 - IF NOT EXISTS INSERT ELSE UPDATE
Cause I'm still getting a "The Compound statement SQL construct or statement is not supported." Syntax error even though it seems to be...
Read more >5 Tips to Write Better Conditionals in JavaScript
1 if/else statement that filter out invalid condition; 3 levels of nested if statement (condition 1, 2 & 3). A general rule I...
Read more >C#" List.First(t=>t.Field.Contains("") " grammar conflict
Debugging shows that if contains exists in the list, an exception will not be thrown, and if it does not exist, an exception...
Read more >Contextual suggestions for SQL syntax
In CockroachDB v23.1, we are introducing a new feature which enables external SQL query editors to provide contextual suggestions on SQL ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I like Derp’s idea at first blush. It would apply to many more cases than just Test-Path.
if -not (expr) { ... }
would indeed be consistent with theswitch
statement grammar.An alternative would be to introduce a negated
if
statement like perl’sunless
: