0

I use Ajax (Jquery) quite often to send forms off for processing in ColdFusion. I send the form to a CFC which returns results and errors back to the user through Ajax notificatitons.

The forms can be quite large (think full HTML pages with additional inputs) and require a lot of logic in the CFC to process correctly depending on what options were selected in the form.

Because each function within the CFC can be large (maybe 1200 lines of code), I am getting the dreaded "branch target offset too large for short" error from ColdFusion. To get around this I have put some code into a .cfm file and used <cfinclude> to get the code back into the cfc which 'solves' the problem but I feel is confusing when trying to organise all the little snippets to associate with a particular CFC. It also may be an inefficient way of working.

I would like to know how other ColdFusion users structure/handle processing forms using CFCs which do a lot of additional stuff while getting the form data into a database.

Some options I have thought of are:

  • Make 'shell' functions for Create, Update, Read, and Delete actions which don't have much code in them
  • Within the shell functions cfinclude all the code snippets by sub-function from many other .cfm files to keep the code to a minimum
  • OR invoke other CFCs which do the sub-functions and pass in the form variables as arguments to them

The options above will end up with me having a structure like this (which I don't like):

Article.cfc (CreateArticle, EditArticle etc)
  CreateArticle_InsertImage.cfc
  CreateArticle_ProcessBodytext.cfc
  CreateArticle_InsertUser.cfc
  CreateArticle_CheckIfExistingArticle.cfc
  EditArticle_UpdateImage.cfc
  EditArticle_UpdateBodytext.cfc
  EditArticle_CheckIfExistingArticle.cfc

I would end up creating a new CFCs for each function rather than them being classes/objects in their own right. They could be CFM files instead (and use <cfinclude> but it seems wierd to do this like this. Is there a alternative/better/standard way anyone knows of?

Shawn
  • 4,758
  • 1
  • 20
  • 29
volume one
  • 6,800
  • 13
  • 67
  • 146
  • What version of ColdFusion are you using? Also based on this article: https://www.coldfusionmuse.com/index.cfm/2007/9/28/Branch.Target.Offset you may have too many `if`s or loops and Java is just going nuts with it. – James A Mohler Jan 09 '18 at 23:06
  • Are you using transactions? I have seen long blocks of `cftransaction` creating this issue. – rrk Jan 10 '18 at 09:24
  • @JamesAMohler Using ColdFusion 2016 Enterprise. There is a lot of cfif tags but I can't bring them down further. I tried reducing them, and remove the `` tags but it didn't work. The only way was to syphon off code into .cfm files and then reconstruct them back together in the CFC using ``. That got me thinking there may be a better/modular/oop way of doing this. – volume one Jan 10 '18 at 11:06
  • 2
    I don't use ajax or cfc's this way. However, something that is generally considered to be a good programming practice is that functions should do only one thing. If you have functions that are over 1000 lines long, you might not be following that practice. – Dan Bracuk Jan 10 '18 at 12:49

1 Answers1

2

Large chunks of cfif/cfelseif/cfif or cfswitch/cfcase can cause the branch offset error. We fixed this in some cases by just moving these to individual cfif statements. This severely reduces the amount of branching in your code.

Breaks:

<cfif ListFindNoCase("myString", trim(arguments.event_key)) GT 0>
   <!--- myString code --->
<cfelseif ListFindNoCase("myOtherString", trim(arguments.event_key)) GT 0>
   <!--- myOtherString code --->
</cfif>

Compiles:

<cfif ListFindNoCase("myString", trim(arguments.event_key)) GT 0>
   <!--- myString code --->
</cfif>
<cfif ListFindNoCase("myOtherString", trim(arguments.event_key)) GT 0>
   <!--- myOtherString code --->
</cfif>

You can move these conditional checks to separate functions, especially if you're ultimately just setting the value of a single variable.

If you're in a bind, just move the code to includes and break it down later. 1200 lines of code in a single function is nothing. My last gig had CFCs that were just piles of includes, extending other CFCs that were piles of includes and each include could contain 20-30k lines of code with functions that contained includes as well. The includes in the functions were often done to specifically address the branch-offset problem.

FWIW, I have a print out of a deep child object's meta-data dump on my wall as abstract art.

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44