ColdFusion
coding and naming
   conventions
Who Am I?

Steven Peeters
Adobe Certified Instructor / consultant at Silver Lining

• Adobe Flex, AIR, Flash Catalyst & ColdFusion Certified Instructor
• Adobe Community Professional
• ColdFusion User Group Manager
• 15 years of development experience
• Flex & AIR since 2004, ColdFusion since 2009
• Author for Friends of ED

 Email:        steven@silver-lining.be
 LinkedIn:     www.linkedin.com/in/stevenpeeters
 Twitter:      @aikisteve
 Blog:         www.flexpert.be
 Website:      www.silver-lining.be
Agenda
3 sections


• Coding conventions
• Naming conventions
• Performance considerations
Conventions

• These conventions are just that: conventions
• Not mandatory for ColdFusion to work
• Lots of them are just common sense
Why do we need this?

• Collaboration
• Maintenance
• Structure
Coding Conventions
Use the application.cfc
Use the application.cfc

• Use Application.cfc instead of Application.cfm
Use the application.cfc

• Use Application.cfc instead of Application.cfm
   • Better structured
Use the application.cfc

• Use Application.cfc instead of Application.cfm
   • Better structured
   • Can be extended
Use the application.cfc

• Use Application.cfc instead of Application.cfm
   • Better structured
   • Can be extended
   • Generic handling of events
onError event handler
onError event handler

   • Application.cfc has onError
     handler
onError event handler

   • Application.cfc has onError
     handler
    • Capture errors at highest level
onError event handler

   • Application.cfc has onError
     handler
    • Capture errors at highest level
    • Both system and user errors
onError event handler

   • Application.cfc has onError
     handler
     • Capture errors at highest level
     • Both system and user errors
   • Use only as last resort/failsafe
ColdFusion Components (CFC)
ColdFusion Components (CFC)

• Use CFCs as much as possible for
ColdFusion Components (CFC)

• Use CFCs as much as possible for
   • Business logic
ColdFusion Components (CFC)

• Use CFCs as much as possible for
   • Business logic
   • Services
ColdFusion Components (CFC)

• Use CFCs as much as possible for
   • Business logic
   • Services
   • Internal operations (e.g. calculations)
Custom Tags
Custom Tags

• Using CFCs does not mean custom tags are
  deprecated
Custom Tags

• Using CFCs does not mean custom tags are
  deprecated
• Use custom tags for reusable output formatting
Reuse existing code
Reuse existing code

• Reuse existing code as much as possible
Reuse existing code

• Reuse existing code as much as possible
   • CFCs
Reuse existing code

• Reuse existing code as much as possible
   • CFCs
   • Custom tags
Reuse existing code

• Reuse existing code as much as possible
   • CFCs
   • Custom tags
   • User Defined Functions (UDF)
<cfqueryparam>
<cfqueryparam>

• Always use <cfqueryparam> in queries, even when
  the parameters are not from user input.
<cfqueryparam>

• Always use <cfqueryparam> in queries, even when
  the parameters are not from user input.
   • Query gets cached (prepared statement)
<cfqueryparam>

• Always use <cfqueryparam> in queries, even when
  the parameters are not from user input.
   • Query gets cached (prepared statement)
   • Parameter type is checked
<cfqueryparam>

• Always use <cfqueryparam> in queries, even when
  the parameters are not from user input.
   • Query gets cached (prepared statement)
   • Parameter type is checked
   • Values get escaped
<cfqueryparam>

• Always use <cfqueryparam> in queries, even when
  the parameters are not from user input.
    • Query gets cached (prepared statement)
    • Parameter type is checked
    • Values get escaped
• Helps to prevent SQL Injection attacks!
<cfproperty>

• Use the <cfproperty> tag to define CFC
  properties
• accessors = “true” will automatically generate
  getters and setters
<cfproperty>
<cfproperty>

• Disable generation of getter method
<cfproperty>

• Disable generation of getter method
   • <cfproperty getter=”false”>
<cfproperty>

• Disable generation of getter method
   • <cfproperty getter=”false”>
• Disable generation of setter method
<cfproperty>

• Disable generation of getter method
   • <cfproperty getter=”false”>
• Disable generation of setter method
   • <cfproperty setter=”false”>
Encapsulate functionality
Encapsulate functionality

• Not all methods of a CFC need to be accessed by
  others
Encapsulate functionality

• Not all methods of a CFC need to be accessed by
  others
• Encapsulation ensures only the necessary
  functionality can be executed by other
  components
Encapsulate functionality
Encapsulate functionality

• Use “access” attribute of <cffunction> tag to
  determine encapsulation level
Encapsulate functionality

• Use “access” attribute of <cffunction> tag to
  determine encapsulation level
• 4 encapsulation levels
Encapsulate functionality

• Use “access” attribute of <cffunction> tag to
  determine encapsulation level
• 4 encapsulation levels
   • Private
Encapsulate functionality

• Use “access” attribute of <cffunction> tag to
  determine encapsulation level
• 4 encapsulation levels
   • Private
   • Protected
Encapsulate functionality

• Use “access” attribute of <cffunction> tag to
  determine encapsulation level
• 4 encapsulation levels
   • Private
   • Protected
   • Public
Encapsulate functionality

• Use “access” attribute of <cffunction> tag to
  determine encapsulation level
• 4 encapsulation levels
   • Private
   • Protected
   • Public
   • Remote
Keep order in your CFC
Keep order in your CFC

• Always use the same order of defining properties
  and methods
Keep order in your CFC

• Always use the same order of defining properties
  and methods
   • Easier collaboration
Keep order in your CFC

• Always use the same order of defining properties
  and methods
   • Easier collaboration
   • Easier maintenance
Keep order in your CFC

• Always use the same order of defining properties
  and methods
   • Easier collaboration
   • Easier maintenance
   • Logical structure
Keep order in your CFC
Keep order in your CFC

• Properties
Keep order in your CFC

• Properties
• Constructor / init method
Keep order in your CFC

• Properties
• Constructor / init method
• Methods
Keep order in your CFC

• Properties
• Constructor / init method
• Methods
   • Remote
Keep order in your CFC

• Properties
• Constructor / init method
• Methods
   • Remote
   • Public
Keep order in your CFC

• Properties
• Constructor / init method
• Methods
   • Remote
   • Public
   • Protected
Keep order in your CFC

• Properties
• Constructor / init method
• Methods
   • Remote
   • Public
   • Protected
   • Private
Keep order in your CFC

• Properties
• Constructor / init method
• Methods
   • Remote
   • Public
   • Protected
   • Private
Use exact names when referring types
Use exact names when referring types

• ColdFusion will search for the proper CFC in a
  general reference
Use exact names when referring types

• ColdFusion will search for the proper CFC in a
  general reference
   • Takes longer to find the type
Use exact names when referring types

• ColdFusion will search for the proper CFC in a
  general reference
   • Takes longer to find the type
   • Could lead to conflicts or wrong type being
      referred
Use exact names when referring types

• ColdFusion will search for the proper CFC in a
  general reference
   • Takes longer to find the type
   • Could lead to conflicts or wrong type being
      referred
   • Can take a long time to find the problem
Use exact names when referring types

• ColdFusion will search for the proper CFC in a
  general reference
    • Takes longer to find the type
    • Could lead to conflicts or wrong type being
      referred
    • Can take a long time to find the problem
• Immediately clear where to find it with fully
  qualified CFC name
Use exact names when referring types
Use exact names when referring types

• General reference
Use exact names when referring types

• General reference
   • e.g. BookingRecord
Use exact names when referring types

• General reference
   • e.g. BookingRecord
• Fully qualified reference
Use exact names when referring types

• General reference
   • e.g. BookingRecord
• Fully qualified reference
   • e.g. be.silverlining.models.BookingRecord
Methods with optional parameters
Methods with optional parameters

• Default parameters
Methods with optional parameters

• Default parameters
• Default value when not filled out
Methods with optional parameters

• Default parameters
• Default value when not filled out
• Mandatory parameters first
Exception handling

• Surround exception prone code with
  <cftry>...<cfcatch>
   • Database
   • Webservice
   • File system access
   • ...
• One <cftry> can have multiple <cfcatch> blocks
Exception handling
Exception handling

• Exceptions should be handled as quickly as
  possible
Exception handling

• Exceptions should be handled as quickly as
  possible
• User should be informed about the exception
Exception handling

• Exceptions should be handled as quickly as
  possible
• User should be informed about the exception
   • Problem solving
Exception handling

• Exceptions should be handled as quickly as
  possible
• User should be informed about the exception
   • Problem solving
   • Bug reports
Exception handling

• Exceptions should be handled as quickly as
  possible
• User should be informed about the exception
   • Problem solving
   • Bug reports
   • Helpdesk
Closing tags
Closing tags

• In general, ColdFusion tags do not need to be
  closed with “/>”
Closing tags

• In general, ColdFusion tags do not need to be
  closed with “/>”
• In some cases, closing a tag can result in
  unexpected behaviour (e.g. custom tags)
Black box components
Black box components

• Components should be written as black boxes
  with an “API” for setting and retrieving values
Black box components

• Components should be written as black boxes
  with an “API” for setting and retrieving values
• Other components do not have to have
  knowledge of the component’s internal workings
Black box components

• Components should be written as black boxes
  with an “API” for setting and retrieving values
• Other components do not have to have
  knowledge of the component’s internal workings
• Allows for better reusability in all circumstances
External CSS file
External CSS file

• Place all CSS styling in one or more external CSS
  files
External CSS file

• Place all CSS styling in one or more external CSS
  files
    • Centralized styling rules
External CSS file

• Place all CSS styling in one or more external CSS
  files
    • Centralized styling rules
    • Easier maintenance
Use source versioning

• You should always use a source versioning system,
  even when you’re working alone
   • Keep track of changes
   • Easily revert changes
   • Merge changes
   • Branching for release
   • ...
Comment your commits
Comment your commits

• When committing changes to the repository
Comment your commits

• When committing changes to the repository
   • Always provide a comment
Comment your commits

• When committing changes to the repository
   • Always provide a comment
   • Provide tracking number when using a task/
      issue tracker
Comment your commits

• When committing changes to the repository
   • Always provide a comment
   • Provide tracking number when using a task/
      issue tracker
   • Easy to find related files for a change
Do not commit broken code
Do not commit broken code

• Any code that is committed to the central
  repository MUST function properly
Do not commit broken code

• Any code that is committed to the central
  repository MUST function properly
• Committing broken code will hinder other
  developers
Do not commit broken code

• Any code that is committed to the central
  repository MUST function properly
• Committing broken code will hinder other
  developers
• Development could come to a full stop!
Test
Test


TEST
    TEST
        TEST
Comment your code
Comment your code

• Code should have ample comments about its
  functionality
Comment your code

• Code should have ample comments about its
  functionality
   • Don’t need to understand code to understand
      its purpose
Comment your code

• Code should have ample comments about its
  functionality
   • Don’t need to understand code to understand
      its purpose
   • Easier collaboration
Comment your code

• Code should have ample comments about its
  functionality
   • Don’t need to understand code to understand
      its purpose
   • Easier collaboration
   • Easier on yourself
Comment you code
Comment you code

• use <!--- ... ---> to comment your code
Comment you code

• use <!--- ... ---> to comment your code
   • ColdFusion style comment
Comment you code

• use <!--- ... ---> to comment your code
   • ColdFusion style comment
   • Not visible in generated HTML
Comment you code

• use <!--- ... ---> to comment your code
   • ColdFusion style comment
   • Not visible in generated HTML
   • Not visible to end user
Organise code in packages

• Structure your code files by placing components in
  different packages
• In CF, package = directory
• Easier to maintain
• Easier to find the right file
Use a framework
Use a framework

• Frameworks help organise your project
Use a framework

• Frameworks help organise your project
• Frameworks help structure your code
Use a framework

• Frameworks help organise your project
• Frameworks help structure your code
• Most common CF frameworks
Use a framework

• Frameworks help organise your project
• Frameworks help structure your code
• Most common CF frameworks
   • Fusebox
Use a framework

• Frameworks help organise your project
• Frameworks help structure your code
• Most common CF frameworks
   • Fusebox
   • ColdBox
Know when not to use a framework

Sometimes, with smaller projects, using a framework
generates more overhead than you get back from
structuring the code.
Naming conventions
Packages
Packages

• Use reverse domain name package structure
Packages

• Use reverse domain name package structure
• Package names are always lowercase
Packages

• Use reverse domain name package structure
• Package names are always lowercase
• e.g. be.silverlining.hotels.models.BookingRecord
Group assets
Group assets

• Use an “assets” package to organise all your assets
Group assets

• Use an “assets” package to organise all your assets
   • fonts
Group assets

• Use an “assets” package to organise all your assets
   • fonts
   • images
Group assets

• Use an “assets” package to organise all your assets
   • fonts
   • images
   • sounds
Group assets

• Use an “assets” package to organise all your assets
   • fonts
   • images
   • sounds
   • logos
Group assets

• Use an “assets” package to organise all your assets
   • fonts
   • images
   • sounds
   • logos
   • ...
Use plural for package names
Use plural for package names

• Package/directory names should always be plural
Use plural for package names

• Package/directory names should always be plural
• In 99,999% you will place multiple components,
  assets or configuration files in 1 directory
Use plural for package names

• Package/directory names should always be plural
• In 99,999% you will place multiple components,
  assets or configuration files in 1 directory
• e.g. models, services, components, views...
Naming CFCs
Naming CFCs

• ColdFusion components should always be a noun
Naming CFCs

• ColdFusion components should always be a noun
• The name should always be singular
Naming CFCs

• ColdFusion components should always be a noun
• The name should always be singular
   • Represents 1 object
Naming CFCs

• ColdFusion components should always be a noun
• The name should always be singular
   • Represents 1 object
• Camelcase/titlecase with first letter uppercase
Naming CFCs

• ColdFusion components should always be a noun
• The name should always be singular
   • Represents 1 object
• Camelcase/titlecase with first letter uppercase
• e.g. BookingRecord
What is camelcase/titlecase?
What is camelcase/titlecase?




• Multiple words stitched together into 1 word
What is camelcase/titlecase?




• Multiple words stitched together into 1 word
• Every word-part begins with uppercase letter
What is camelcase/titlecase?




• Multiple words stitched together into 1 word
• Every word-part begins with uppercase letter
• First letter uppercase or lowercase
Naming methods
Naming methods

• ColdFusion methods should always contain an
  active verb
Naming methods

• ColdFusion methods should always contain an
  active verb
• Name should represent its purpose
Naming methods

• ColdFusion methods should always contain an
  active verb
• Name should represent its purpose
• Descriptive
Naming methods

• ColdFusion methods should always contain an
  active verb
• Name should represent its purpose
• Descriptive
• Camelcase/titlecase with first letter lowercase
Naming methods

• ColdFusion methods should always contain an
  active verb
• Name should represent its purpose
• Descriptive
• Camelcase/titlecase with first letter lowercase
• e.g. getBookingRecord
Naming properties
Naming properties

• ColdFusion properties should always be a noun
Naming properties

• ColdFusion properties should always be a noun
• Name can be singular or plural
Naming properties

• ColdFusion properties should always be a noun
• Name can be singular or plural
   • Singular for 1 object
Naming properties

• ColdFusion properties should always be a noun
• Name can be singular or plural
   • Singular for 1 object
   • Plural for lists, arrays...
Naming properties

• ColdFusion properties should always be a noun
• Name can be singular or plural
   • Singular for 1 object
   • Plural for lists, arrays...
• Name should be descriptive
Naming properties
Naming properties

• Camelcase/titlecase with first letter lowercase
Naming properties

• Camelcase/titlecase with first letter lowercase
• Use underscore at end for properties used for
  multi-language/dynamic labels
Naming properties

• Camelcase/titlecase with first letter lowercase
• Use underscore at end for properties used for
  multi-language/dynamic labels
• e.g. firstName, zipCode, travelGuide, firstName_, ...
Performance considerations
isDefined()
isDefined()

• Cautiously use the isDefined() method
isDefined()

• Cautiously use the isDefined() method
• Starts a fuzzy search on all relevant scopes
isDefined()

• Cautiously use the isDefined() method
• Starts a fuzzy search on all relevant scopes
• Replace with structKeyExists()
isDefined()

• Cautiously use the isDefined() method
• Starts a fuzzy search on all relevant scopes
• Replace with structKeyExists()
• Use scope in isDefined()
evaluate()
evaluate()

• Evaluate dynamic variables (e.g. array loops)
evaluate()

• Evaluate dynamic variables (e.g. array loops)
• Should be avoided at all costs
evaluate()

• Evaluate dynamic variables (e.g. array loops)
• Should be avoided at all costs
• e.g. evaluate(“form.field#i#”)
Comparing items
Comparing items

• Compare 1 variable with multiple values
Comparing items

• Compare 1 variable with multiple values
   • listFind
Comparing items

• Compare 1 variable with multiple values
   • listFind
   • listFindNoCase
Comparing items

• Compare 1 variable with multiple values
   • listFind
   • listFindNoCase
   • check if result equals 0
Comparing items

• Compare 1 variable with multiple values
   • listFind
   • listFindNoCase
   • check if result equals 0
• e.g. <cfif listFindNoCase(“a,b,c”,x) IS NOT 0>
Query caching
Query caching

• Use caching on queries that don’t change often
Query caching

• Use caching on queries that don’t change often
   • cachedWithin
Query caching

• Use caching on queries that don’t change often
   • cachedWithin
   • cachedAfter
Query caching

• Use caching on queries that don’t change often
   • cachedWithin
   • cachedAfter
• Conserves bandwidth between servers
Query caching

• Use caching on queries that don’t change often
   • cachedWithin
   • cachedAfter
• Conserves bandwidth between servers
• Executes faster once it’s cached
<cfcache>
<cfcache>

• Use <cfcache> for caching complete page results
<cfcache>

• Use <cfcache> for caching complete page results
• Caches on disk
<cfcache>

• Use <cfcache> for caching complete page results
• Caches on disk
• Timespan definition
<cfcache>

• Use <cfcache> for caching complete page results
• Caches on disk
• Timespan definition
• Use primarily on “static” pages
createObject() vs <cfinvoke>
createObject() vs <cfinvoke>

• <cfinvoke> creates new objects on each call
createObject() vs <cfinvoke>

• <cfinvoke> creates new objects on each call
• createObject() gives you instantiated object
createObject() vs <cfinvoke>

• <cfinvoke> creates new objects on each call
• createObject() gives you instantiated object
• Cache in scopes for reuse
createObject() vs <cfinvoke>

• <cfinvoke> creates new objects on each call
• createObject() gives you instantiated object
• Cache in scopes for reuse
   • Smaller memory footprint
createObject() vs <cfinvoke>

• <cfinvoke> creates new objects on each call
• createObject() gives you instantiated object
• Cache in scopes for reuse
   • Smaller memory footprint
   • Reusability
createObject() vs <cfinvoke>

• <cfinvoke> creates new objects on each call
• createObject() gives you instantiated object
• Cache in scopes for reuse
   • Smaller memory footprint
   • Reusability
   • Faster execution time
Multi threading
Multi threading

• Use <cfthread> to start separate thread
Multi threading

• Use <cfthread> to start separate thread
• Use it for asynchronous processes
Multi threading

• Use <cfthread> to start separate thread
• Use it for asynchronous processes
• Use named locks for synchronising certain steps
Multi threading

• Use <cfthread> to start separate thread
• Use it for asynchronous processes
• Use named locks for synchronising certain steps
• e.g. database reads, news feeds, sending email...
Avoid <cfoutput> for static content
Avoid <cfoutput> for static content

• Use only for dynamic content
Avoid <cfoutput> for static content

• Use only for dynamic content
• Searches static content
Avoid <cfoutput> for static content

• Use only for dynamic content
• Searches static content
• Searching takes time
Avoid <cfoutput> for static content

• Use only for dynamic content
• Searches static content
• Searching takes time
• Multiple <cfoutput> blocks is better!
Using pound signs (#)
Using pound signs (#)




                   #
Using pound signs (#)

• Use # signs around variables




                                 #
Using pound signs (#)

• Use # signs around variables
   • In strings




                                 #
Using pound signs (#)

• Use # signs around variables
   • In strings




                                 #
   • Between quotes
Using pound signs (#)

• Use # signs around variables
   • In strings




                                 #
   • Between quotes
   • In <cfoutput>
Do not use pound signs (#)
Do not use pound signs (#)




                      #
Do not use pound signs (#)

• Do not use # around variables




                                  #
Do not use pound signs (#)

• Do not use # around variables
   • In code




                                  #
Do not use pound signs (#)

• Do not use # around variables
   • In code




                                  #
   • In conditional statements
Do not use pound signs (#)

• Do not use # around variables
   • In code




                                  #
   • In conditional statements
   • Calculations
Do not use pound signs (#)

• Do not use # around variables
   • In code




                                  #
   • In conditional statements
   • Calculations
   • Parameters
Twitter:                Blog:
      @aikisteve           www.flexpert.be

       Email:             Company website:
steven@silver-lining.be   www.silver-lining.be

More Related Content

KEY
Frederick web meetup slides
PDF
Invoke dynamic your api to hotspot
PDF
An introduction and future of Ruby coverage library
PDF
Ro r trilogy-part-1
PDF
Exception Handling: Designing Robust Software in Ruby (with presentation note)
PDF
Exception Handling: Designing Robust Software in Ruby
PPT
Our Universe
PPTX
Electromagnetic Radiation & its effect on human
Frederick web meetup slides
Invoke dynamic your api to hotspot
An introduction and future of Ruby coverage library
Ro r trilogy-part-1
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby
Our Universe
Electromagnetic Radiation & its effect on human

Viewers also liked (18)

PPTX
laws of radiation
PPT
Zenerdiodes
PPT
Chapter 1 blackbody radiation
PPT
cold fusion ppt
PPTX
Zener diode voltage regulator (ALIV - Bangladesh)
PPTX
Electromagnetic Radiation
PPTX
THE UNIVERSE
PPT
Universe ppt 6 B
PPTX
Different types of galaxies
PPT
Basic logic gates
PPTX
astronomy: Types of galaxies
PPTX
PPT
Theories Of The Universe
PPT
Galaxy presentation
PPTX
Project on Solar Energy
PPT
Presentation on solar cell
PPTX
Blue Brain
laws of radiation
Zenerdiodes
Chapter 1 blackbody radiation
cold fusion ppt
Zener diode voltage regulator (ALIV - Bangladesh)
Electromagnetic Radiation
THE UNIVERSE
Universe ppt 6 B
Different types of galaxies
Basic logic gates
astronomy: Types of galaxies
Theories Of The Universe
Galaxy presentation
Project on Solar Energy
Presentation on solar cell
Blue Brain

Similar to Coding and naming conventions (20)

KEY
Attributes, reflection, and dynamic programming
ZIP
44 Slides About 22 Modules
PDF
Use Docker to Enhance Your Testing
PDF
Modern Functional Fluent CFML REST by Luis Majano
KEY
CakePHP 2.0 - PHP Matsuri 2011
KEY
SOTR 2012
PDF
CDI Best Practices with Real-Life Examples - TUT3287
PPTX
ColdFusion Internals
PDF
Shift Remote: Mobile - Efficiently Building Native Frameworks for Multiple Pl...
KEY
drupal 7 amfserver presentation: integrating flash and drupal
KEY
Advanced Server Integration with Data and Direct
PDF
Cool Tools for Customizing (Websites) - Ver1
PDF
Securing Legacy CFML Code
PPTX
Introduction to Monsoon PHP framework
KEY
Scotch On The Rocks 2011
PDF
ColdFusion Features for More Modern Coding
PDF
Cfml features modern_coding
PPTX
Tooling for the productive front-end developer
PPTX
Ember - introduction
PDF
BSides Lisbon 2013 - All your sites belong to Burp
Attributes, reflection, and dynamic programming
44 Slides About 22 Modules
Use Docker to Enhance Your Testing
Modern Functional Fluent CFML REST by Luis Majano
CakePHP 2.0 - PHP Matsuri 2011
SOTR 2012
CDI Best Practices with Real-Life Examples - TUT3287
ColdFusion Internals
Shift Remote: Mobile - Efficiently Building Native Frameworks for Multiple Pl...
drupal 7 amfserver presentation: integrating flash and drupal
Advanced Server Integration with Data and Direct
Cool Tools for Customizing (Websites) - Ver1
Securing Legacy CFML Code
Introduction to Monsoon PHP framework
Scotch On The Rocks 2011
ColdFusion Features for More Modern Coding
Cfml features modern_coding
Tooling for the productive front-end developer
Ember - introduction
BSides Lisbon 2013 - All your sites belong to Burp

Coding and naming conventions

Editor's Notes