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?