Eryk Dwornicki - Blog

Hello! My name is Eryk, and this is my home page! You can also find me here:

< Return to home

Clean code tips #3 — Avoid redundant conditions

Less code to read is often the best solution. If a function returns bool you can often immediately return the condition directly instead of making if..else blocks.

const float MaxObjectMassToGrabKg = 50.0f;

// Before
bool AJanuszCharacter::IsAbleToGrabComponent(UPrimitiveComponent* Component) const
{
	if (Component->CalculateMass() > MaxObjectMassToGrabKg)
	{
		return false;
	}
	return true;
}

// After
bool AJanuszCharacter::IsAbleToGrabComponent(UPrimitiveComponent* Component) const
{
	return (Component->CalculateMass() <= MaxObjectMassToGrabKg);
}