Ensure null-safety is precise: guard against $null and empty results and blank strings (including blank entries inside arrays/hashtables). Also treat “omitted optional/switch parameters” as meaningful (absence != explicit value), and structure conditions so short-circuiting prevents null dereferences.
Apply these rules:
-not [string]::IsNullOrWhiteSpace($x) before using them as keys/identifiers.$x (not null) and a non-zero count when “at least one” is required.'' or whitespace entries), not only the container.$a -and $b) so later expressions aren’t evaluated when earlier values are null/invalid.Example patterns:
# 1) Dictionary/key safety
if ($runtime -and -not [string]::IsNullOrWhiteSpace($runtime.Name)) {
AddRuntimeToDictionary -Runtime $runtime -RuntimeToVersionDictionary ([Ref]$RuntimeToVersionLinux)
} else {
$nullKeySafeSkip = $true
}
# 2) Assertions: non-null != useful
Assert-NotNull $crrJob
Assert-True { @($crrJob).Count -gt 0 }
# 3) Per-entry validation inside a collection
foreach ($id in $UserAssignedIdentity) {
if ([string]::IsNullOrWhiteSpace($id)) {
throw "At least one user-assigned identity resource ID must be provided via -UserAssignedIdentity."
}
}
# 4) Switch/optional parameter semantics (absence means “not enabled”)
# If you need identity disabled, omit the switch; if you need it enabled, pass it.
if ($EnableSystemAssignedIdentity) {
$functionAppDef.IdentityType = 'SystemAssigned'
}
# else: treat as None/no identity configured
Adopting this reduces null-key crashes, avoids false positives in tests (empty arrays), and prevents unintended behavior changes caused by omitted optional/switch parameters.