2013-03-09 73 views
1

我有一堆cfc文件(運行coldfusion8),其中包含一個cfswitch捆綁類似的功能(用戶,搜索,...)。如何在Coldfusion中處理「無效的方法代碼長度」?

有些CFC文件變得過大,所以我收到Invalid method Code length 72645我假設說,「你的文件太大,無法解析」 ..

我通常在約達到這個2000行,並認爲這是...並不多。

由於我在一堆文件上碰到了這個上限,我正在考慮添加另一個功能層=從switch-statement中刪除所有函數,並使用每個函數單獨的​​cfc調用cfinvoke

問:
我的應用程序並不大,所以我不知道,有沒有辦法來規避「你 - 不能具備的,更比2000線-IN-A -cfc「的上限,如果不是,那麼在應用程序中調用每個主要方法的獨立CFC /組件是否可行?

謝謝!

編輯:回覆:「有計劃地」 :-)
目前我氟氯化碳的結構是這樣的:

<cfcomponent extends="controllers.main" output="false" hint="handle all user interactions">  
    <cfscript> 
     VARIABLES.Instance.Validation = { 
      // all user-relate form fields including validation method to call (pass = no validation) 
      id="spec_id" 
      , corp="pass" 
      ... 
     }; 
    </cfscript> 

    <cffunction name="Init" access="public" returntype="any" output="false" hint="Initialize form data"> 
     <cfreturn true /> 
    </cffunction> 
    <cffunction name="Defaults" access="public" returntype="struct" output="false" hint="Assign defaults"> 
     <cfscript> 
      // form default values assigned to instance 
      var formDefaults = { 
       id="" 
      , comp="" 
      ... 
      }; 
     </cfscript> 
     <cfreturn formDefaults /> 
    </cffunction> 

    <cffunction name="Commit" access="remote" returntype="struct" output="false" hint="Main handler"> 
     <cfscript> 
     // all var declarations 
     var userID = ""; 
     var strRememberMe = ""; 
     var timestamp = now(); 
     ... 
     var defaultValues = THIS.Defaults(); 
     var LOCAL = {}; 

     structAppend(defaultValues, VARIABLES.Instance.FormData); 
     LOCAL.User = defaultValues; 
     LOCAL.User.timestamp = timestamp ; 
     </cfscript> 

     <!--- the switch --->  
     <cfswitch expression = #LOCAL.User.submitted_form#> 

      ... lot of stuff ... 

     </cfswitch> 

     <cfreturn LOCAL.Response /> 
    </cffunction> 

    <!--- UTILITY FUNCTIONS ---> 
    <cffunction name="Validate" access="public" returntype="array" output="false" hint="validate form inputs"> 
     <cfscript> 
     var LOCAL = {}; 
     var double = structNew(); 
     double.criteria = VARIABLES.Instance.Validation; 
     double.form = VARIABLES.Instance.FormData; 
     </cfscript> 

     <!--- Get error name and type ---> 
     <cfinvoke component="form_validate" method="validate_fields" double="#double#" returnvariable="validation_errors"></cfinvoke> 
     <cfset LOCAL.ErrorMessages = validation_errors /> 
     <cfreturn LOCAL.ErrorMessages /> 
    </cffunction>         
</cfcomponent> 

現在,我已經寫了很多非結構化的東西,但分裂的functional- cfcs然後像這樣處理它們對我來說似乎不是「非計劃」的。

如果是這樣,那麼更好的方法來設置它,因爲我不得不重新執行它?交換機將有大約15個案例,這是我使用的所有主要cfcs的平均值。

謝謝!

+2

而不是擔心編譯器的上限(和2000行等,不是硬性限制,它不是那麼簡單),你可能更好地重新組合你的CFC和方法有點多一點。呃......「計劃中」。看看子類的東西,看看你的方法是否太複雜了,等等(方法應該做一件事)。 – 2013-03-09 20:44:33

+0

我以爲我的CFC是「計劃好的」:-)給我一分鐘,我張貼一個簡短的草圖。子類化意味着什麼? – frequent 2013-03-09 21:03:45

+0

@AdamCameron:好的。我開始通過從交換機調用另一個功能層來分離我的交換機。我想這很接近子類,是嗎? – frequent 2013-03-10 00:37:29

回答

3

我前段時間在CF8中遇到過這個問題。沒有通用的「2000行限制」,而是在JVM中爲地址偏移量的最大值,以便在子例程內跳轉。偏移量不得超過2個字節(WORD),否則您將面臨此異常。爲了避免子程序(函數)中的大地址偏移,您需要儘量減少大塊條件跳轉(if/else/switch)。您可以通過使用多個子例程來完成此操作(這些調用可能需要最多4/8字節的完整寄存器)。

例如:重新設計...

function A (x, y) { 
    if (...) { 
     switch (...) { 
      case ...: 
       switch (...) { 
        ... 
       } 
      ... 
     } 
    } else { 
     switch (...) { 
      ... 
     } 
    } 
} 

喜歡的東西......

function A (x, y) { 
    if (...) { 
     call B(x, y); 
    } else { 
     call C(x, y); 
    } 
} 

function B (x, y) { 
    switch (...) { 
     case ...: 
      call B1(x, y); 
     ... 
    } 
} 

function B1 (x, y) { 
    switch (...) { 
     .... 
    } 
} 

function C (x, y) { 
    switch (...) { 
     .... 
    } 
} 

...你的想法。這通常也會提高可讀性和可維護性。

+0

啊。我懂了。就我現在正在做的事情而言,只將B,B1,C分開來分開文件。謝謝! – frequent 2013-03-10 09:50:40

相關問題