Tip of the Day – When to use include

A little while ago I posed the following question to the flexcoders mailing list,

I just wanted to ping everyone and get their opinion on something. Why would anyone ever want to use the include directive? I’ve recently been working on poorly designed project where at the top of every module there is an “include Header.as” statement that has 30 include statements within it. To give a datagrid additional functionality, you give it an id=”myDataGrid”, and the include statement takes care of the rest. It’s really bad.

I just don’t see a good use for the include statement anymore. In my opinion, it just promotes bad programming practices.

I mean, can’t everything be taken care of using OOP methodologies? Thoughts?

I’d like to thank everyone who responded, you all gave me quite a bit of insight.

After thinking about this for a while now, here is my list of the common reasons people incorrectly use include (include directive).

I have common functionality that I want shared across multiple components.

That’s great that you’re using DRY philosophy! However, it’s much better to combine the DRY philosophy with object-oriented programming practices. Instead of using the include directive, abstract out common functionality into a parent class. Then you can create children of your parent component, and add additional functionality as needed.

I’m building a framework.

Great… don’t use include. See above reason.

I want to use Code-Behind.

Ted Patrick wrote a wonderful post on Code-Behind in Flex 2. He accomplishes Code-Behind, and does so in an object-oriented manner without using include statements.

Adobe doesn’t natively allow multiple inheritance in AS3, and using the include directive is the only way to fake it.

Frankly, if you understand that statement… you’re advanced enough to know what you’re doing, and you don’t need this tip.

== Conclusion ==

There are rare occasions where include is exactly what you need. However, if you still feel you have a valid reason to use the include directive on a regular basis, please leave a comment below and explain yourself.

Related Posts

Comments Closed