关于范围和封装的问题
我有一个关于范围和封装的一般性问题。采取两个场景:
场景 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 ...
}
}
场景 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 ...
}
}
从功能上讲,这两个代码片段执行完全相同的操作。我试图了解编码风格的细节以及为什么一种方式可能比另一种方式更受青睐?从封装的角度来看,似乎场景2更好。但场景 1 更加万无一失,因为条件中的布尔值仅来自一个地方,即全局常量。您不必担心函数调用在正确接收参数时可能会传入错误的值。但场景 2 似乎值得,因为您消除了常量的依赖性,并且可以让函数表现得更加动态。对此有什么想法吗?我还有其他考虑的权衡吗?
相同的概念和问题也适用于对象和类。但为了简化代码示例,我只是以函数的形式呈现该示例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第二种方法中,您可以使
_myFunction
存在于一个单独的模块中,而不依赖于全局模块 - 因此它更容易测试,更容易重用,并帮助您控制依赖关系图,这通常在大型代码库中成为一个严重的问题。如果您插入可以轻松避免的依赖项,那么您只会使依赖关系图问题变得更糟,并且几乎没有潜在的好处可以为此付出代价。事实上,为了获得这种优势,一个很好的依赖模式是显式 INJECT 对象,否则该对象会在模块之间创建(通常是不希望的和不希望的)依赖关系 - 请参阅 此处作为开始。作为测试、松散耦合和重用的狂热者,我也逐渐成为依赖注入的狂热者,所以我不会梦想访问全局常量,而将其作为参数传递是一个明显的替代方案......;- )。
In the second approach you can make
_myFunction
live in a separate module, with no dependency from the global one -- so it's easier to test, easier to reuse, and helps you control your dependency graph, which often becomes a serious problem in large codebases. If you insert dependencies you could easily avoid, you're only making the dependency-graph problem worse, and very few potential benefits could ever possibly pay for THAT.Indeed, to gain this kind of advantages, a great dependency pattern is to explicitly INJECT object that would otherwise create (generally undesirable and undesired) dependencies between modules -- see here for a start. As a fanatic of testing, loose coupling, and reuse, I'm gradually becoming a fanatic of dependency injection too, so I wouldn't DREAM of accessing a global constant where passing it as an argument is an obvious alternative...;-).
如果您希望将相同的编译单元链接到两个版本(特别是作为全局固定路径中的共享库),或者如果您在同一进程中运行多个实例,则首选 2。否则,如果您处于从源代码重建所有内容都没有障碍的情况下,#1 更好。
有些事情确实是全球性的。全局常量一点也不危险。
2 would be preferred if you wanted the same compilation unit to be linked against both versions (especially as a shared library in a globally fixed path), or if you ran multiple instances in the same process. Otherwise, if you're in a situation where rebuilding everything from source is no obstacle, #1 is better.
Some things really are global. Global constants are not dangerous at all.