Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-04-2010, 08:44 PM   #1
glantucan
newbe forever!
 
glantucan's Avatar
 
Join Date: Oct 2004
Location: Madrid, Spain
Posts: 88
Default Constructor functions are cluster bombs

I would like to know your opinion on what Bill Sanders, one of the co-authors of "ActionScript 3 Design Patterns" says about not using constructors at all, here is asummary of his original post Constructors functions are Cluster bombs:

* "If you do not include one, you’re not tempted to add properties and methods in the constructor that might generate dependencies. Your client isn’t a problem because its just making requests and none of the other classes instantiate your client."
* "With “parameterized” classes, instead of passing a parameter to the class, it’s less binding to pass it just to the method that uses it."
* "If everything in a class is needed immediately by the constructed instance, first instantiate an instance and then call a method through the instance that fires off the whole crew."

Personally, I am starting to think he is right. Hey! He (among others) wrote the most interesting book I've red about AS3. In fact I think I will read it again just when I finish it. But as with the book, the concepts related loose-coupling between classes, are a litle difficult to grasp for me. Yet
__________________
glantucan
glantucan is offline   Reply With Quote
Old 12-05-2010, 01:45 PM   #2
drkstr
Flexpert
 
drkstr's Avatar
 
Join Date: Sep 2006
Location: Seattle, WA: USA
Posts: 1,587
Default

Yeah, I would agree that constructors are bad for some classes (especially view classes). They're fine for simple things like a model, or a custom event. However, view classes usually get created in stages, or as part of a life-cycle such as the case with Flex. Lots of logic in the constructor also makes them harder to extend. In general, it's usually best to break up functionality into as many chunks as possible, which is not just limited to the constructor. For exmple, you may have an init function which just bootstraps more specific functions like initStyles(), initModel(), etc.
drkstr is offline   Reply With Quote
Old 12-05-2010, 05:11 PM   #3
glantucan
newbe forever!
 
glantucan's Avatar
 
Join Date: Oct 2004
Location: Madrid, Spain
Posts: 88
Default

Yup, I understand that,... I think

But what I don't get very well, hoaw could you not use constructors at all. In a MVC pattern you have, at least, to tell the Views which controllers and models to use, and I don't see how they are less coupled with them by doing it through another method instead of the constructor.
__________________
glantucan
glantucan is offline   Reply With Quote
Old 12-05-2010, 05:30 PM   #4
the binary
Senior Member
 
the binary's Avatar
 
Join Date: May 2003
Location: berlin | germany
Posts: 960
Default

Quote:
..at least, to tell the Views which controllers and models to use..
this could be done by e.g. programming to interfaces like..

ActionScript Code:
package  {     public interface IAbstractView     {         function setModel (model:IAbstractModel);         function getModel () :IAbstractModel;                 function setController (): IAbstractController;         function getController (): IAbstractController;     } }

if any view is implementing this interface you don't have to deal with the constructor..
please note, this is just an 'dirty example' but should point you in the direction..
__________________
8bm
the binary is offline   Reply With Quote
Old 12-05-2010, 06:07 PM   #5
glantucan
newbe forever!
 
glantucan's Avatar
 
Join Date: Oct 2004
Location: Madrid, Spain
Posts: 88
Default

Ok, I see. That way you can change models and controllers even at runtime if they implement IAbstractModel and IAbstractController.

But if you include this methods while initializing them in the constructor, why are the classes more coupled. Still don't see it. Perhaps I didn't get the "coupling" thing at all.
For instance
ActionScript Code:
public class myView { public function myView(model:IAbstractModel, controller:IAbstractController) { ...} function setModel (model:IAbstractModel):void{ ...} function getModel () :IAbstractModel{ ...} function setController (controller:IAbstractController):void{ ...} function getController (): IAbstractController{ ...} ... }

Oh!, maybe... without initialization that class could work without the need of models and controllers instantiated. Is that the reason?

Tanks guys, I'm learning a lot
__________________
glantucan
glantucan is offline   Reply With Quote
Old 01-10-2011, 11:05 PM   #6
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

I've read some stuff before by that guy and I kind of thought he's not the greatest person to be giving advice on design patterns or even programming. Take it with a metric ton of salt....

There are definitely occasions where constructors are needed such as adapters and commands to name a quick one.
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");

Last edited by Flash Gordon; 01-11-2011 at 05:28 AM.
Flash Gordon is offline   Reply With Quote
Old 01-11-2011, 02:26 AM   #7
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 1,571
Default

Quote:
Originally Posted by Flash Gordon View Post
Take it with a metric ton of salt....
I would agree with the grain of salt appraisal. Basic operator precedence isn't something you should be surprised by after so many years.
__________________
http://www.scriptocalypse.com - It's not the end of the world. It's just Actionscript!

man.mask = mask_mc;

Last edited by maskedMan; 01-11-2011 at 03:36 PM.
maskedMan is offline   Reply With Quote
Old 01-11-2011, 05:25 AM   #8
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

you're right, harsh words. Allow me to edit them and you may as well.
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Old 01-11-2011, 01:10 PM   #9
newblack
dondeEstanMisPantalones?
 
newblack's Avatar
 
Join Date: Nov 2005
Location: New York Proper
Posts: 1,355
Send a message via AIM to newblack
Default

well based on that "operator precedence" post, i'm inclined to agree with calling him an idiot, but that doesn't change the fact that i can't think of one decent reason you should ever need a constructor--architecturally or otherwise.
__________________
i am gibreel farishta
general relativity
jellytanks alpha redux
newblack is offline   Reply With Quote
Old 01-11-2011, 03:36 PM   #10
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 1,571
Default

Quote:
Originally Posted by Flash Gordon View Post
you're right, harsh words. Allow me to edit them and you may as well.
No problem.
__________________
http://www.scriptocalypse.com - It's not the end of the world. It's just Actionscript!

man.mask = mask_mc;
maskedMan is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:28 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.