When a value is optional (or can differ by feature/region/constructor), never assume it’s present or non-empty. Apply consistent null/empty/whitespace handling, gate context-dependent logic, and validate after any transform (e.g., JSON/model conversion) so missing fields don’t silently change behavior.
Apply this standard:
null; use string.IsNullOrWhiteSpace for string parameters.null/empty; throw a clear PSInvalidOperationException (or equivalent) for invalid combinations.Pattern example (normalization + post-mapping validation):
string changeReference = boundParameters.TryGetValue("-ChangeReference", out var v)
? v as string
: null;
changeReference = string.IsNullOrWhiteSpace(changeReference) ? null : changeReference;
// Example for mapping/bridge where fields can be lost
var json = JsonConvert.SerializeObject(restoreRequest);
var mapped = JsonConvert.DeserializeObject<MyCrrRestoreRequest>(json);
if (mapped == null || mapped.TargetDetails == null || string.IsNullOrEmpty(mapped.SourceResourceId))
{
throw new InvalidOperationException("Mapping lost required fields (TargetDetails/SourceResourceId). Update the bridge models.");
}
This prevents NullReferenceExceptions, incorrect defaults, and silent behavior changes caused by missing/empty inputs or divergent model mappings.