English 中文(简体)
一致性和可维持性
原标题:Constants and Maintainability

我有一个简单的问题,涉及良好的方案拟订做法。 具体地说,我很想知道处理集团间固定装置的适当方式。 在学校里,他们教导我们把我们的常识放在他们被宣布为有名人名的档案的顶端(通常是一个班级档案,教授也有一些变化)。 现在,我已经看到,在工业的多个地方,所有固定装置均已停用,并输入一个大的包括型文档。

在第一种情况下,由于手机游戏守则必须迅速进入令人惊讶的多种装置,这为从中工作提供了集中的位置,所以应当停止。 但后来,我发现,重复这种做法是一种完全不同的设想(公用事业内部法典),没有说明为什么这样做的理由(主要是,“由于这是他总是怎样做的”。

因此,最佳做法是什么? 我知道这似乎很不舒服,但我始终被告知,早日开始善良习惯是取得成功的关键。

最佳回答

对所有不变值进行同等处理通常是过分简单化。 每个人都有不同的含义,我对待他们的方式也不尽相同。 下面是一小小片背景,即我如何处理这方面的不变。 如果你认为有更好的办法,请更正我。

  • 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.

其他不变国家有自己的背景。 我愿把常态移出法典,因为尽管这有悖常理,但常态倾向于change

出于同样的原因,把所有固定装置放在一个大档案中是坏的。 请允许我说一下,你们的集会只取决于十大不变。 即便在不断的Y变迁的情况下,也必须对大会进行改组,这一变化应影响到大会。

问题回答

为了提供过于简化的答复,我认为最好把他们放在你希望找到他们的地方。 计算、分类。 http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29”rel=“nofollow”>cohesion principle 。 其优点是,在需要时,你会更容易发现这些障碍。 你可以很容易地把你需要的固定点包括在内,而不会把你的名字空间与其他未使用的名称空间污染。

另一项重要说明是,如果可能的话,将集团相关常数编成群。 例如,调和的常数将升入大气。





相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

Design pattern for managing queues and stacks?

Is there a design pattern for managing a queue or a stack? For example, we are looking to manage a list of tasks. These tasks will be added to a group queue, users will then be able to pull off the ...

Organizing classes using the repository design pattern

I have started upgrading one of our internal software applications, written in ASP.NET Web Forms, and moving to ASP.NET MVC. I am trying to leverage the Repository design pattern for my classes, ...

Misuse of Observer Pattern?

I have a Car object which contains a latitude field and a longitude field. I use the observer pattern so that any time either of these fields change in my application, my car object is notified. I ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...