DEV Community

Cover image for Our way: Constants
Robson TenĂłrio
Robson TenĂłrio

Posted on

Our way: Constants

👉 Go to Summary

There is no wrong or right way, there is our way.

-- Someone

.
.
.

Magic number

WTF!?

class Costumer 
{
   public float $amount;

   public function something() 
   {
       // Ok, and WTF means 98.45 ?
       if ($this->amount > 98.45) {
          ...
       }

   }

}

Enter fullscreen mode Exit fullscreen mode

Nice!

class Costumer 
{
   public float $amount;

   public const LIMIT = 98.45

   public function something() 
   {
       if ($this->amount > self::LIMIT) {
          ...
       }

   }

}
Enter fullscreen mode Exit fullscreen mode

Nice!

// If LIMIT is used across codebase put it on `.env`
LIMIT = 98.45


public function something() 
{
    if ($this->amount > config("app.limit")) {
          ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay