diff --git a/src/PowerShellTips/2024-01-07-use-null-coalescing-to-avoid-nulls-and-null-checks.ps1 b/src/PowerShellTips/2024-01-07-use-null-coalescing-to-avoid-nulls-and-null-checks.ps1 new file mode 100644 index 0000000..c41e922 --- /dev/null +++ b/src/PowerShellTips/2024-01-07-use-null-coalescing-to-avoid-nulls-and-null-checks.ps1 @@ -0,0 +1,49 @@ +$tip = [tiPS.PowerShellTip]::new() +$tip.CreatedDate = [DateTime]::Parse('2024-01-07') +$tip.Title = 'Use null-coalescing to avoid nulls and null checks' +$tip.TipText = @' +PowerShell 7.0 introduced the null-coalescing operator (??). This can help reduce the number of explicit null checks you need to write. + +The null-coalescing operator syntax is as follows: ?? + +If the value on the left-hand side of the operator is not null, the statement on the right-hand side of the operator will not be evaluated. + +Null-coalescing can be used in expressions, conditional (if) statements, and variable assignments. +'@ +$tip.Example = @' +# $x is null so the value on the right-hand side of the operator is returned. +> $x = $null +> $x ?? 'Default value for x' +Default value for x + +# $y is not null so the value on the left-hand side of the operator is returned. +> $y = 'Explicit value for y' +> $y ?? 'Default value for y' +Explicit value for y + +# The null-coalescing operator can be used in variable assignments. +# Since $x is null, $z is assigned the value of $y. +> $z = $x ?? $y +> $z +Explicit value for y + +# Use the above expression ($z = $x ?? $y) instead of: +if ($null -eq $x) { + $z = $y +} +'@ +$tip.Urls = @( + 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.4#null-coalescing-operator-' + 'https://toastit.dev/2020/03/10/ps7now-null-conditional/' +) +$tip.Category = [tiPS.TipCategory]::Syntax # Community, Editor, Module, NativeCmdlet, Performance, Security, Syntax, Terminal, or Other. + +# Community: Social events and community resources. e.g. PowerShell Summit, podcasts, etc. +# Editor: Editor tips and extensions. e.g. VSCode, ISE, etc. +# Module: Modules and module tips. e.g. PSScriptAnalyzer, Pester, etc. +# NativeCmdlet: Native cmdlet tips. e.g. Get-Process, Get-ChildItem, Get-Content, etc. +# Performance: Tips to improve runtime performance. e.g. foreach vs ForEach-Object, ForEach-Object -Parallel, etc. +# Security: Security tips. e.g. ExecutionPolicy, Constrained Language Mode, passwords, etc. +# Syntax: Syntax tips. e.g. splatting, pipeline, etc. +# Terminal: Terminal shortcuts and tips. e.g. PSReadLine, Windows Terminal, ConEmu, etc. +# Other: Tips that don't fit into any of the other categories. diff --git a/src/tiPS/PowerShellTips.json b/src/tiPS/PowerShellTips.json index 55c016a..12d5ea9 100644 --- a/src/tiPS/PowerShellTips.json +++ b/src/tiPS/PowerShellTips.json @@ -454,5 +454,16 @@ "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_if#using-the-ternary-operator-syntax" ], "Category": 6 + }, + { + "CreatedDate": "2024-01-07T00:00:00", + "Title": "Use null-coalescing to avoid nulls and null checks", + "TipText": "PowerShell 7.0 introduced the null-coalescing operator (??). This can help reduce the number of explicit null checks you need to write.\r\n\r\nThe null-coalescing operator syntax is as follows: ?? \r\n\r\nIf the value on the left-hand side of the operator is not null, the statement on the right-hand side of the operator will not be evaluated.\r\n\r\nNull-coalescing can be used in expressions, conditional (if) statements, and variable assignments.", + "Example": "# $x is null so the value on the right-hand side of the operator is returned.\r\n> $x = $null\r\n> $x ?? 'Default value for x'\r\nDefault value for x\r\n\r\n# $y is not null so the value on the left-hand side of the operator is returned.\r\n> $y = 'Explicit value for y'\r\n> $y ?? 'Default value for y'\r\nExplicit value for y\r\n\r\n# The null-coalescing operator can be used in variable assignments.\r\n# Since $x is null, $z is assigned the value of $y.\r\n> $z = $x ?? $y\r\n> $z\r\nExplicit value for y\r\n\r\n# Use the above expression ($z = $x ?? $y) instead of:\r\nif ($null -eq $x) {\r\n $z = $y\r\n}", + "Urls": [ + "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.4#null-coalescing-operator-", + "https://toastit.dev/2020/03/10/ps7now-null-conditional/" + ], + "Category": 6 } ]