Always use consistent and safe patterns when dealing with potentially null or undefined values. This includes using safe casting methods, explicit null checks, proper initialization, and immutable variables after validation.
Key practices:
try_as<T>()
instead of as<T>()
to avoid runtime exceptions when casting might failconst auto
when storing results of null checks to prevent accidental modificationExample from the codebase:
// Inconsistent - uses try_as in one place but as in another
if (const auto newTermArgs = _ContentArgs.try_as<NewTerminalArgs>())
{
return newTermArgs->GetArgCount() + gsl::narrow<uint32_t>(_argDescriptors.size());
}
// Should also use try_as here for consistency
return _ContentArgs.as<NewTerminalArgs>()->GetArgDescriptorAt(index - additionalArgCount);
// Better approach - consistent safe casting
if (const auto newTermArgs = _ContentArgs.try_as<NewTerminalArgs>())
{
return newTermArgs->GetArgDescriptorAt(index - additionalArgCount);
}
// Proper null handling in serialization
if (json.isNull())
{
return MediaResource::FromString(L"");
}
// Proper initialization to prevent undefined behavior
UINT _systemMenuNextItemId = 0; // Instead of leaving uninitialized
This pattern prevents runtime crashes, undefined behavior, and makes code more predictable and maintainable.
Enter the URL of a public GitHub repository