Return early when a precondition fails
Check invalid states first so the main work does not end up buried inside nested branches.
bool UWeaponComponent::TryFire(const FVector& AimDirection)
{
if (!IsValid(OwnerCharacter))
{
return false;
}
if (AmmoCount <= 0 || IsOnCooldown())
{
return false;
}
const FVector FireDirection = AimDirection.GetSafeNormal();
if (FireDirection.IsNearlyZero())
{
return false;
}
SpawnProjectile(FireDirection);
--AmmoCount;
StartCooldown();
return true;
}- Why
- You can read the preconditions from top to bottom, then follow the successful path without tracking several levels of indentation. The function does not change state until every check has passed.
- Costs
- Multiple returns make manual cleanup easy to miss. Keep cleanup in scoped Unreal or C++ types so it happens regardless of which return runs.
- Instead
- Move shared checks into a named validation function when several call sites follow the same rules. A single exit still makes sense when an external API requires centralized cleanup.

