I have a generic question about scope and encapsulation. Take two scenarios:
Scenario 1:
// a global application level constant
public static const IS_DEMO_MODE:Boolean = false;
... // somewhere deep in the codebase
private function _myFunction():void
{
if (IS_DEMO_MODE == true) {
// If Demo Mode do not allow this function to complete
return;
}
else {
// Function behaves normally
// Code ...
}
}
Scenario 2:
// a global application level constant
public static const IS_DEMO_MODE:Boolean = false;
... // somewhere deep in the codebase
// call the function and pass in the constant
_myFunction(IS_DEMO_MODE);
private function _myFunction(isDemoMode:Boolean):void
{
if (isDemoMode == true) {
// If Demo Mode do not allow this function to complete
return;
}
else {
// Function behaves normally
// Code ...
}
}
Functionally speaking these two code snippets do the same exact same thing. I am trying to understand the finer points of coding style and why one way might be preferred over the other? It seems that scenario 2 is better from an encapsulation point of view. But scenario 1 is more foolproof in that the boolean that is in the conditional comes from one place only, the global constant. You don't have to worry about a function call that while correctly receiving a parameter could pass in the wrong value. But scenario 2 seems worthwhile because you remove the dependency of the constant and can have the function behave more dynamically. Any thoughts on this? Are there any other trade offs I am over looking?
Same concept and question applied to objects and classes as well. But I just present the example in terms of a function for simplicity of the code example.