Default Event Constructor Parameters used in Subclasses

The base Event class in Flash has 3 parameters.

public function Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)

The first parameter, type, is required. The second two, bubbles and cancelable, default to false. This means that you don’t have to pass either of these two parameters and they will incur values.

If you look at any of the native Flash Event subclass constructors, their additional parameters follow the Event base’s default parameters and have defaults.

public function IOErrorEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, text:String = "", id:int = 0)

So the questions arise when you subclass an event and you have a new parameter you consider required: Should all additional parameters be required to follow the base parameters to ahere to the base signature? Should a developer have to type in values that almost a 100% of the time will be defaulted in order to type in one that is thought to be required? Obviously, if this is the case then the additional parameters can not be required by the compiler and only enforced by practice. These parameters would have to have defaults.

// required parameters following default ones is illegal and the compiler will flag it
public function Foo(defaultParam:int = 0, requiredParam:int)

While working on our media framework, I debated on whether it was valid to follow Flash’s native Event subclasses and make required parameters only required by practice or whether to change the subclass signature and make them required by nature. I settled on the fact that it’s ok to switch up the order of parameters in the constructor’s signature and place new required parameters before base default ones.

// super constructor
public function Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
// subclass constructor
public function DownloadEvent(type:String, currentProgress:Number, totalProgress:Number, bubbles:Boolean = false, cancelable:Boolean = false)

If the parameters were all defaults, a developer could forget a “required” one and end up having to debug unexpected results. By actually requiring parameters, a developer who forgets to put them in then will get warned by the compiler. This seems better practice and also feels more natural when programming. For example, when creating new instances, you only have to pass in required parameters and can let the base ones default.

 this.dispatchEvent = new DownloadEvent(DownloadEvent.DOWNLOAD_STARTED, .5, .5)

Personally, I don’t think signature familiarity is as important as context responsibility. It also doesn’t make sense to force yourself into a practice that can actually be less productive and hurt you rather than be beneficial. That’s my take.

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">