常数和可维护性

发布于 2024-09-30 16:54:39 字数 338 浏览 2 评论 0原文

我有一个与良好编程实践相关的简单问题。具体来说,我很好奇处理分组常量的正确方法。在学校里,他们教我们将常量放在文件的顶部,在文件中首先声明它们(通常是班级文件,教授有一些变化)。现在,我在业界的几个地方看到,所有常量都被拉出来并卷入一个大的包含类型文件中。

在第一种情况下,提取常量是有意义的,因为这是手机游戏的代码,必须快速移植到各种令人惊叹的设备上,这提供了一个集中的工作场所。但是,后来,我发现这种重复的做法是一个完全不同的场景(公用事业的内部代码),几乎没有理由说明为什么会这样(基本上,“因为这就是他一直这样做的方式”)。

那么,最佳实践是什么?我知道这可能看起来很平常,但我总是被告知,尽早养成良好的习惯是成功的关键。

I have a simple question that relates to good programming practices. Specifically, I am curious to the proper way to handle grouping constants. In school, they taught us to place our constants at the top of the file in which they are fist declared (usually a class file and there is some variation by professor). Now, I have seen in several places in the industry in which ALL constants are pulled out and rolled into one big include-type file.

In the first case, it made sense to pull constants out as this was code for cellphone games that had to be rapidly ported to an amazing variety of devices and this provided a centralized place to work from. But, later on, I find this practice repeated is a completely different scenario (In-house code for a public utility) with little justification as to why this is so (basically, "because that is how he have always done it").

So, what would the best practices be? I know it may seem mundane, but I have always been told that starting good habits early is the key to success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你另情深 2024-10-07 16:54:39

对所有常量一视同仁通常过于简单化。每一个在其上下文中都有不同的含义,我对待它们的方式也相应地有所不同。下面是一个小表,其中列出了上下文以及我如何处理此上下文中的常量。如果您认为有更好的方法,请纠正我。

  • 配置参数:=>尽可能从代码中提取到配置文件或数据库。
  • 用户界面字符串:=>在资源文件中(以便于维护和本地化)。
  • 可能的枚举:枚举将多个常量很好地包装在同一上下文中(例如,一周中的几天、连接状态...)。我通常将定义放在所使用的同一个包中,或者如果多个程序集/组件将使用它,则将其放在共享库中。
  • 全局/静态对象:我检查我的设计,看看是否可以应用 singletonfactory 模式(或在 DI 框架)。
  • 测试期望:为了可读性,我通常在测试方法中将它们保留原样。
  • “神奇”数字:我很少需要使用它们;可能会将它们留在语义相关的类中。

其他常量有其自己的上下文。我喜欢将常量移出代码,因为无论听起来多么矛盾,常量往往会改变

出于同样的原因,将所有常量放在一个大文件中也是不好的。假设您的一个程序集仅依赖于常量 X。即使常量 Y 更改,该程序集也必须重新编译,并且此更改应该不会影响它。

Treating all constants equally is usually an oversimplification. Each one has a different meaning in its context, and the way I treat them varies accordingly. Below is a small table of contexts vs how I handle constants in this context. Please correct me if you think there is a better approach.

  • Configuration parameters: => out of code wherever possible, to a configuration file or database.
  • UI strings: => in a resource file (for easy maintenance and localization).
  • Possible enums: Enums wrap multiple constants in the same context nicely (for example, days of the week, connection state...). I usually put the definition in the same package as it's used, or in a shared library if multiple assemblies/components will use it.
  • Global/static objects: I review my design to see if singleton or factory patterns can be applied (or register the object in a DI framework).
  • Test expectations: I usually leave them as they are, in the test method, for readability.
  • "Magic" numbers: I rarely have to use them; would possibly leave them in a semantically related class.

Other constants have their own contexts. I like to move constants out of code since, however paradoxical it may sound, constants tend to change.

For the same reason, putting all constants in one big file is bad. Let's say one of your assemblies depend only on constant X. This assembly has to be recompiled even when constant Y changes, and this change should not have affected it.

无敌元气妹 2024-10-07 16:54:39

为了给出一个过于简化的答案,我认为最好将它们放在您期望找到它们的地方。意思是,对它们进行分类。这涉及到内聚原则。这样做的优点是,当您需要它们时,您会更容易地找到它们。您可以轻松地仅包含所需的常量,而不会因其他未使用的常量而污染您的命名空间。

另一个重要的注意事项是,如果可能的话,将相关常量分组到枚举中。例如,对齐常量将进入枚举 Align。

To give an over simplified answer, I believe it's best to place them where you would expect to find them. Meaning, categorize them. This relates to the cohesion principle. Advantages of this are that you will find them easier when you need them. You can easily include only the constants you need, without polluting your namespace with other unused ones.

Another important note is, if possible, group related constants into enums. E.g. alignment constants would go into an enum Align.

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