1

In our Adobe Coldfusion project we have some components used as singletons such as this one:

component name="Util" {
    public function init() {
        variables.settings = loadFromConig();
        variables.prefix = "my_";
        return this;
    }

    public string function getPrefix() {
        return variables.prefix;
    }

    public struct function getSettings() {
        return variables.settings;
    }
}

This should normally work because the init() function must always be executed first in order to be able to call any other method. My concern is: is there any risk that this does not happen as expected (for example under stress testing conditions) if the loadFromConig() function takes too long and some getter method throws an undefined variable exception?

In general, is it a good practice to define some 'constants' using the variables' scope in the constructor (Method A) or is it better to use properties with default values instead (Method B) or is it safer to hardcode the 'constants' in the getters directly:

public string function getPrefix() {
   return "my_";
}

(Method C)?

I we use Method A - is it a good idea to put a cflock around these lines:

  variables.settings = loadFromConig();
  variables.prefix = "my_";

in the Constructor or would such a lock be useless?

  • In my experience using the VARIABLE scope is ok, but I would move it outside the of the init() method because there is a nothing I'm aware in ColdFusion that will inherently call your init() method when the component is created. That said you might consider moving them to the THIS scope if you want them to be accessible directly from outside the component. No need for in my opinion. – Jason Holden Mar 07 '21 at 23:34
  • The constructor (init function) is always implicitly called for example calls the init() in service.MyService This is how it is at least on Adobe Coldfusion 2018. Isn't it so in your case too? Are you using Adobe CF or Lucee? – Milena Dimitrova Mar 11 '21 at 12:17
  • Milena can you link to the Adobe documentation stating that the init() method is implicitly called when a component is created? I know this is true for most languages, but I was not aware this was a ColdFusion feature. – Jason Holden Mar 12 '21 at 13:10
  • 1
    Here is an Example from Adobe showing how the init() function gets called without explicitly writing it - thanks to the keyword "new": https://helpx.adobe.com/coldfusion/developing-applications/building-blocks-of-coldfusion-applications/object-oriented-programming-coldfusion.html Invocation with "new": bunny = new Animal('small','rabbit'); this calles the constructor of the "Animal" component. – Milena Dimitrova Mar 17 '21 at 08:59

1 Answers1

1

After some rethinking of the issue I came to the conclusion that it is better to use final variables as constants, because you cannot change the value of a final variable once you initialize it.

component{
    final prefix = "my_";         
    function checkPrefix(){
        writeoutput("prefix: " & prefix);
    }
}