skip to content
Rizkhal Lamaau

How to make better condition

/ 1 min read

As an developer, theree will be a time your code will be legacy to other developers or you will working with team, to make everything easy to understand, never make any condition difficult to understand. Like

if ($payment === 1) { }

if ($payment === 2) { }

When the code become to legacy or your team will contribute to the project, he will say What the hell is that? and he will spend a lot of time to investigate what the fuck condition mean. So instead of you being sworn by your friend, do something like this

// declare readable function
private function isPaymentAccepted()
{
   return $this->payment === 1;
}

private function isPaymentPending()
{
   return $this->payment === 2;
}

// using the readable function
if ($this->isPaymentPending()) {
   //
}

if ($this->isPaymentAccepted()) {
   //
}

Your code will looks better and easy to understand right now 🤘