question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Improve syntax of "if not exist"

See original GitHub issue

Related 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:open
  • Created 7 years ago
  • Reactions:3
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
rkeithhillcommented, Aug 20, 2016

if !(expr) { statements* } if -not (expr) { statements* }

I like Derp’s idea at first blush. It would apply to many more cases than just Test-Path.

5reactions
IISResetMecommented, Aug 20, 2016

if -not (expr) { ... } would indeed be consistent with the switch statement grammar.

An alternative would be to introduce a negated if statement like perl’s unless:

unless (expr) { ... } else { ... }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found