关于范围和封装的问题

发布于 2024-08-04 21:23:35 字数 1191 浏览 8 评论 0原文

我有一个关于范围和封装的一般性问题。采取两个场景:

场景 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2024-08-11 21:23:35

在第二种方法中,您可以使 _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...;-).

最冷一天 2024-08-11 21:23:35

如果您希望将相同的编译单元链接到两个版本(特别是作为全局固定路径中的共享库),或者如果您在同一进程中运行多个实例,则首选 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文