List Info

Thread: Re: Action files




Re: Action files
user name
2007-04-20 10:12:40
Having 500 lines per method is a code smell, i.e. a signpost you should refactor. The best start to this is splitting your monster method (start method by method) into smaller bitesize private functions. It's easy to do, just start by putting each new step or block of functionality in the code into a new private method with a name which describes that separated functionality.

Once you have that break up in place, do the same for other monsters. Once all the monsters are reduced to a few dozen imps (okay, the analogy is getting thin ;)) you probably have places where they overlap - i.e. another sign to combine smaller methods where are too similar, or doing the same things, or duplicate the same code over and over again.

After that exercise, then (and only then) should you consider splitting code into other controllers, or a controller/action setup.

If that's not desireable (and honestly even if it is), just grab all those private methods and create a new class (I'd call it a "Controller Helper") or even classes. Then you can create new instances of these to use in your controller methods - and hey presto your controller code is now much reduced with all the complicated, and potentially reusable code in a nice separate family of classes - a mini-library of sorts.

Hopefully this shines a little light on how to keep controllers minimal by refactoring duplicate/complex code into helper classes or libraries.

Regards,
Pádraic
 
Pádraic Brady
http://blog.astrumfutura.com
http://www.patternsforphp.com



----- Original Message ----
From: Matthew Weier O'Phinney <matthewzend.com&gt;
To: fw-generallists.zend.com
Sent: Friday, April 20, 2007 12:08:03 PM
Subject: Re: [fw-general] Action files

-- agatone <zoran.zokigmail.com&gt; wrote
(on Friday, 20 April 2007, 02:28 AM -0700):
&gt; I'm sorry if this was already discussed but i could find any topics about it.
> Let's say we have in our controllers folder this action file :
>
> class IndexController extends Zend_Controller_Action {
>&nbsp;  function indexAction() { ... }
>&nbsp;  function addAction() { ... }
>&nbsp;  function removeAction() { ... }
>&nbsp;  function editAction() { ... }
>&nbsp;  function deleteAction() { ...}
>&nbsp;  .... etc. ...
> }
>
> So at all this actions file could easily have 500+ lines, what is (in my
> oponion) very bad.
> Wouldn't be easier if actions could be in separate files - more simple
>; structure ... ?
>
> Any ideas ?

Besides working on the Zend MVC, I'm also the author of Cgiapp, a PHP
port of CGI::Application. In CGI::Application, the rule of thumb is that
if you have more than seven run modes (equivalent to action methods in
ZF), then you need to refactor.

I'd suggest that you should probably refactor such a controller into
multiple controllers with their own subsets of actions.

Alternatively, consider writing your own dispatcher that dispatches to
controller/action.php. This would be fairly trivial to accomplish -- but
is not likely to go into the ZF core any time soon.

--
Matthew Weier O'Phinney
PHP Developer&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;| matthewzend.com
Zend - The PHP Company&nbsp;  | http://www.zend.com/



Ahhh...imagining that irresistible "new car" smell?
Check out new cars at Yahoo! Autos.
Re: Action files
user name
2007-04-20 10:19:53
Hi

I simply extend the controller with my own class:

class Download_IndexController extends
modules_download_classes_Download

And then this class: modules_download_classes_Download
extends my Zend 
Action

class Modules_Download_Classes_Download extends Zend_Action

And then: this extends the main Zend Controller Action

class Zend_Action extends Zend_Controller_Action

So I have a nice nested heirachy of methods per module - and
then per 
site / applciation.


Thus I can place all my modular methods into one file.

I also started with creating one file per basic action - as
controllers, 
I liked the way the URL looked as was easier for me to
remember where to 
look, didnt like the usage of multiple actions in one
controller.

Cheers

Ian

� wrote:
> Having 500 lines per method is a code smell, i.e. a
signpost you should 
> refactor. The best start to this is splitting your
monster method (start 
> method by method) into smaller bitesize private
functions. It's easy to 
> do, just start by putting each new step or block of
functionality in the 
> code into a new private method with a name which
describes that 
> separated functionality.
> 
> Once you have that break up in place, do the same for
other monsters. 
> Once all the monsters are reduced to a few dozen imps
(okay, the analogy 
> is getting thin ;)) you probably have places where they
overlap - i.e. 
> another sign to combine smaller methods where are too
similar, or doing 
> the same things, or duplicate the same code over and
over again.
> 
> After that exercise, then (and only then) should you
consider splitting 
> code into other controllers, or a controller/action
setup.
> 
> If that's not desireable (and honestly even if it is),
just grab all 
> those private methods and create a new class (I'd call
it a "Controller 
> Helper") or even classes. Then you can create new
instances of these to 
> use in your controller methods - and hey presto your
controller code is 
> now much reduced with all the complicated, and
potentially reusable code 
> in a nice separate family of classes - a mini-library
of sorts.
> 
> Hopefully this shines a little light on how to keep
controllers minimal 
> by refactoring duplicate/complex code into helper
classes or libraries. 
> 
> Regards,
> P�draic
>  
> P�draic Brady
> http://blog.astrumfutura
.com
> http://www.patternsforp
hp.com
> 
> 
> ----- Original Message ----
> From: Matthew Weier O'Phinney <matthewzend.com>
> To: fw-generallists.zend.com
> Sent: Friday, April 20, 2007 12:08:03 PM
> Subject: Re: [fw-general] Action files
> 
> -- agatone <zoran.zokigmail.com> wrote
> (on Friday, 20 April 2007, 02:28 AM -0700):
>  > I'm sorry if this was already discussed but i
could find any topics 
> about it.
>  > Let's say we have in our controllers folder this
action file :
>  >
>  > class IndexController extends
Zend_Controller_Action {
>  >   function indexAction() { ... }
>  >   function addAction() { ... }
>  >   function removeAction() { ... }
>  >   function editAction() { ... }
>  >   function deleteAction() { ...}
>  >   .... etc. ...
>  > }
>  >
>  > So at all this actions file could easily have
500+ lines, what is (in my
>  > oponion) very bad.
>  > Wouldn't be easier if actions could be in
separate files - more simple
>  > structure ... ?
>  >
>  > Any ideas ?
> 
> Besides working on the Zend MVC, I'm also the author of
Cgiapp, a PHP
> port of CGI::Application. In CGI::Application, the rule
of thumb is that
> if you have more than seven run modes (equivalent to
action methods in
> ZF), then you need to refactor.
> 
> I'd suggest that you should probably refactor such a
controller into
> multiple controllers with their own subsets of
actions.
> 
> Alternatively, consider writing your own dispatcher
that dispatches to
> controller/action.php. This would be fairly trivial to
accomplish -- but
> is not likely to go into the ZF core any time soon.
> 
> -- 
> Matthew Weier O'Phinney
> PHP Developer            | matthewzend.com
> Zend - The PHP Company   | http://www.zend.com/
> 
> 
>
------------------------------------------------------------
------------
> Ahhh...imagining that irresistible "new car"
smell?
> Check out new cars at Yahoo! Autos. 
> <http://us.rd.yahoo.com/e
vt=48245/*http://autos.yahoo.com/new_cars.html;_ylc=X3oDMTE1
YW1jcXJ2BF9TAzk3MTA3MDc2BHNlYwNtYWlsdGFncwRzbGsDbmV3LWNhcnM-
> 
> 

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )