router icon indicating copy to clipboard operation
router copied to clipboard

Named arguments vs immutable methods

Open xepozz opened this issue 1 year ago • 6 comments

What's the point to create an object every method calls?

Let's take a look at the Route class: image

The coolest thing is you can use auto-complete feature to call any of these methods. Is it all? I think so. Let's imagine an average corporate app with more than 100 routes. Every application bootstrap it create this 100+ routes. But if we count it comes to 100*3.5 created objects: 100 route * 3-4 immutable methods calls, so 350 created object every bootstrap just for the one matched route in the end. That's awful. If we open Route's constructor and let users create it instead we can decrease the created objects numbers back to 100.

class Route {
    public function __construct(
        private string $pattern,
        private array $methods,
        private string $name = null,
        private array $middlewares = [],
}

That's enough to gain performance easy.

So the way to use Route changes as the following:

Route::get('/')
  ->name('site/index')
  ->action([SiteController::index, 'index'])
  ->middlewares([AuthMiddleware::class])
;
new Route(
  pattern: '/',
  name: 'site/index',
  action: [SiteController::index, 'index'],
  middlewares: [AuthMiddleware::class],
);

All of these properties can be readonly, but it's better to later it for the next release. Also all these methods are questionable. Do we really need keep both named arguments and methods?

Due to this changes #179 turns to be real just by one line above the class #[\Attribute]

xepozz avatar Mar 14 '23 07:03 xepozz