Eryk Dwornicki - BlogHello! My name is Eryk, and this is my home page! You can also find me here: |
|
By using early return you eliminate conditions immediately when reading code from top to bottom, don’t need to keep them on the back of your mind
Before
bool HumanBase::WalkTo(const Vector2& position)
{
if (m_state != HumanState::Walking) {
m_currentWalkPath = GetWorld()->FindPath(m_position, position);
if (m_currentWalkPath) {
if (!m_currentWalkPath->GetPointsCount()) {
m_position = position;
m_previousPosition = position;
}
else {
m_targetPathPoint = 0;
m_nextState = m_state;
m_state = HumanState::Walking;
}
return true;
}
}
return false;
}
After
bool HumanBase::WalkTo(const Vector2& position)
{
if (m_state == HumanState::Walking) {
return false;
}
m_currentWalkPath = GetWorld()->FindPath(m_position, position);
if (!m_currentWalkPath) {
return false;
}
if (!m_currentWalkPath->GetPointsCount()) {
m_position = position;
m_previousPosition = position;
return true;
}
m_targetPathPoint = 0;
m_nextState = m_state;
m_state = HumanState::Walking;
return true;
}