Add base controller to avoid boilerplate code

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Post Reply
unbeerable
Registered User
Posts: 5
Joined: Wed Nov 23, 2016 7:10 am

Add base controller to avoid boilerplate code

Post by unbeerable »

I'm facing the same problem every time I create a controller in my extensions. It almost always needs some of @request, @user, @auth, @controller.helper, @dbal.conn and @template services (sometimes all of them), because almost every controller just reads input from @request, performs some checks via @user and @auth, retrieves data via @dbal.conn, sets variables to @template and returns the response via @controller.helper->render() method. This behavior generates a lot of boilerplate code (and I hate boilerplate code :))

We could create an abstract default controller which will have these services and maybe some helper methods inside 'automagically', and then this controller will be extended by our class (of course if we want it). It won't break any BC, because it's just an option.

We probably also can think about extending the list of services which can be injected directly into the action method via type-hinting. Currently it works only for Symfony Request object.

User avatar
javiexin
Registered User
Posts: 90
Joined: Thu Dec 22, 2011 10:04 am

Re: Add base controller to avoid boilerplate code

Post by javiexin »

Maybe better if added to Skeleton Extension?

unbeerable
Registered User
Posts: 5
Joined: Wed Nov 23, 2016 7:10 am

Re: Add base controller to avoid boilerplate code

Post by unbeerable »

javiexin wrote: Wed Apr 19, 2017 1:43 pm Maybe better if added to Skeleton Extension?
Well, you're offering me not to write this code by hands but still to have this code. I want to not have it at all.

User avatar
david63
Registered User
Posts: 355
Joined: Mon Feb 07, 2005 7:23 am

Re: Add base controller to avoid boilerplate code

Post by david63 »

I thought that the whole point of using a "framework" was to just add the blocks that you need as and when you need them.
David
Remember: You only know what you know -
and you do not know what you do not know!

unbeerable
Registered User
Posts: 5
Joined: Wed Nov 23, 2016 7:10 am

Re: Add base controller to avoid boilerplate code

Post by unbeerable »

david63 wrote: Wed Apr 19, 2017 5:55 pm I thought that the whole point of using a "framework" was to just add the blocks that you need as and when you need them.
Why we still have so much helpers and abstract base classes then? Why don't we just make the things from scratch every time we need to do something?
With default controller you're not required to use it, but you can simplify your development process, focusing on things which are more important.

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: Add base controller to avoid boilerplate code

Post by DavidIQ »

Not necessarily arguing against it, but in my experience having a "one size fits all" class or library for such things that many different projects can inherit ends up making that class or library overly complex and means having objects instantiated and properties populated that won't be used at all so you end up with unnecessary overhead.

As an example I have extensions or have seen extensions that will use 1 item from your list and none of the rest. Also seen extensions that don't use any of the items on your list.

"Boilerplate code" is sometimes a necessary and unavoidable evil. You can try to do certain things to reduce it, but you can't completely get rid of it and can end up with overly complex code in trying to avoid it.
Image

unbeerable
Registered User
Posts: 5
Joined: Wed Nov 23, 2016 7:10 am

Re: Add base controller to avoid boilerplate code

Post by unbeerable »

DavidIQ wrote: Wed Apr 19, 2017 6:11 pm Not necessarily arguing against it, but in my experience having a "one size fits all" class or library for such things that many different projects can inherit ends up making that class or library overly complex and means having objects instantiated and properties populated that won't be used at all so you end up with unnecessary overhead.

As an example I have extensions or have seen extensions that will use 1 item from your list and none of the rest. Also seen extensions that don't use any of the items on your list.

"Boilerplate code" is sometimes a necessary and unavoidable evil. You can try to do certain things to reduce it, but you can't completely get rid of it and can end up with overly complex code in trying to avoid it.
Well, we can somehow analyse the needs of developers and find the Golden mean between complexity and universality. I definitely understand your arguments, I see the same problems every day. But I still believe it's possibile to create a compromise solution. To do nothing is worse than to do something.

User avatar
javiexin
Registered User
Posts: 90
Joined: Thu Dec 22, 2011 10:04 am

Re: Add base controller to avoid boilerplate code

Post by javiexin »

unbeerable wrote: Wed Apr 19, 2017 4:02 pm
javiexin wrote: Wed Apr 19, 2017 1:43 pm Maybe better if added to Skeleton Extension?
Well, you're offering me not to write this code by hands but still to have this code. I want to not have it at all.
No, I am not offering you anything, I am suggesting the place where I think this fits best IN MY OPINION, that is not better than anyone else's, but not worse either.

And if you see it so clearly, go ahead, code it, publlish it, and then we can talk about something concrete.

By the way, having the code is not an option if it is needed. The question is where you have it. Boilerplate code, abstract base class or whatever.

Post Reply