-
Notifications
You must be signed in to change notification settings - Fork 24
CFC Primer Part 1: Definition and History
By Matt Woodward (matt at mach-ii.com)
- Introduction
- CFCs: Definition and Brief History
<cfinclude>
- Custom Tags
- User-Defined Functions (UDFs)
- CFCs
- Next parts of this primer
ColdFusion Components (CFCs) serve as the basis for object-oriented programming (OOP) in CFML. Since Mach-II is an OO-based framework, it is crucial to understand CFCs to be able to use Mach-II successfully. This CFC Primer was created to give developers some background and history of the CFML language from the standpoint of enabling modular application development. The primer also intends to introduce developers to the power of CFCs and why it is important to take advantage of CFCs in building applications.
CFCs were introduced in ColdFusion MX 6, which was the first release of ColdFusion on the Java-based platform. CFCs are CFML's version of objects. Although CFCs are not as powerful as Java objects, CFCs eliminate much of the complexity and strictness of Java while providing almost complete OO functionality. CFML is not a fully object-oriented language. CFCs allow CFML developers, however, to build OO applications that architecturally resemble OO applications in Java and other languages that are completely object-oriented. CFML developers can use CFCs to build tiered applications in which presentation logic and business logic are completely separate from each another. This separation of presentation and business logic helps developers create applications that are easier to build and maintain.
The following discussion explains the evolution of modular development in CFML, the precursors of CFCs, and the significance of CFCs in giving CFML developers the power to create modular applications.
<cfinclude>
allows developers to write bits of code in CFML
templates that can be included anywhere within an application.
Prior to ColdFusion 3, <cfinclude>
was essentially the only way to
reuse code in CFML applications. While <cfinclude>
is not very
powerful for writing modular code, it avoids the need to cut and
paste common code within an application. Using <cfinclude>
, a
piece of functionality contained in an included template can easily
be revised in a single place, and these changes will immediately be
reflected in every instance of the included CFML template
throughout an application. Although <cfinclude>
makes code
reusable in a very basic sense, it does not allow for the
development of modular, tiered applications in which presentation
logic and business logic are truly separated.
Custom tags allow developers to customize CFML code that can be
called in similar fashion to built-in CFML tags (e.g.
<cf_mytag>
). They represent a significant advancement over
<cfinclude>
. Custom tags were first introduced in ColdFusion 3,
but they became much more powerful when nested custom tags were
added to ColdFusion 4. Since custom tags have their own scope and
multiple execution modes (start, body, and end), they are powerful
tools for encapsulating CFML code in a reusable, easy-to-use
construct. Custom tags added considerable modular development
capabilities to CFML, but it is still rather difficult to develop
fully modular applications by using only custom tags.
User-defined functions (UDFs), which were introduced in ColdFusion
5, allow CFML developers to write customized functionality that is
more flexible and more discrete than functionality created with
custom tags. While UDFs are not as powerful as custom tags in one
respect, UDFs are more useful for smaller bits of functionality or
logic and have less performance overhead than custom tags. In
ColdFusion 5, UDFs had to be written within a <cfscript>
block.
This limitation did not allow UDFs to include any built-in CFML
tags, thereby limiting their functionality. UDFs allowed users,
however, to build libraries of reusable functions; because of this,
UDFs are still an important aspect of CFML development. ColdFusion
6 enhanced UDFs by adding the <cffunction>
tag to the CFML
language, which allows standard CFML tags to be incorporated into
UDFs. The addition of <cffunction>
, <cfargument>
, and <cfreturn>
to ColdFusion 6 greatly expanded the power and utility of UDFs, and
these tags also serve as the basic building blocks of CFCs.
CFCs, introduced in ColdFusion 6 and greatly enhanced in ColdFusion 6.1, allow CFML developers to create self-contained modular software components that include both functions in the form of UDFs (called "methods" in many other OO languages) as well as object data. The CFML language became much more powerful with the addition of CFCs, because for the first time it became possible to develop fully modular CFML applications. Many CFML developers are still learning how to take advantage of this capability. By combining UDFs with object-specific data in the form of a CFC, it is now possible to build truly modular, tiered, OO applications in CFML.
The combination of behavior (functions) and attributes (data) within a single object allows CFML developers to model real-world objects as software components. Therefore, now it is much easier to conceptualize and develop complex applications in CFML than it was in the past. In addition, CFCs allow developers to create objects that are cohesive (single/purposed) and loosely coupled to other CFCs within an application, which makes applications much more flexible and easier to maintain.