简体   繁体   English

我应该将其存储在数据库还是代码中?

[英]Should I store this in the database or in the code?

I'm creating a small game composed of weapons. 我正在创建一个由武器组成的小游戏。 Weapons have characteristics, like the accuracy. 武器具有准确性等特征。 When a player crafts such a weapon, a value between min and max are generated for each characteristic. 当玩家制作这样的武器时,将为每个特征生成介于最小值和最大值之间的值。 For example, the accuracy of a new gun is a number between 2 and 5. 例如,新枪的精确度是2到5之间的一个数字。

My question is... should I store the minimum and maximum value in the database or should it be hard coded in the code ? 我的问题是...应该将最小值和最大值存储在数据库中还是应该在代码中对其进行硬编码?

I understand that putting them in the database allows me to change these values easily, however these won't change very often and doing this mean having to make a database request when I need these values. 我知道将它们放在数据库中使我可以轻松地更改这些值,但是这些值不会经常更改,这意味着当我需要这些值时必须提出数据库请求。 Moreover, its means having way much more tables... however, is it a good practice to store this directly in the code ? 而且,它的意思是拥有更多的表...但是,将其直接存储在代码中是一种好习惯吗?

In conclusion, I really don't know what solution to chose as both have advantages and disadvantage. 总之,我真的不知道该选择哪种解决方案,因为它们都有优点和缺点。

If you have attributes of an entity, then you should store them in the database. 如果您具有实体的属性,则应将它们存储在数据库中。

That is what databases are for, storing data. 那就是数据库用于存储数据的目的。 I can see no advantage to hardcoding such values. 我看不出对这些值进行硬编码没有好处。 Worse, the values might be used in different places in your code. 更糟糕的是,这些值可能在代码的不同位置使用。 And, when you update them, you might end up with inconsistent values throughout the code. 而且,当您更新它们时,可能会导致整个代码中的值不一致。

EDIT: 编辑:

If these are default values, then I can imagine storing them in the code along with all the other information about the weapon -- name of the weapon, category, and so on. 如果这些是默认值,那么我可以想象将它们与有关武器的所有其他信息(武器名称,类别等)一起存储在代码中。 Those values are the source information for the weapons. 这些值是武器的原始信息。

I still think it would be better to have a Weapons table or WeaponDefaults table so these are in the database. 我仍然认为最好有一个Weapons表或WeaponDefaults表,以便这些都在数据库中。 Right now, you might think the defaults are only used in one place. 现在,您可能会认为默认值仅在一个地方使用。 You would be surprised how software can grow. 您会惊讶于软件如何成长。 Also, having them in the database makes the values more maintainable. 同样,将它们存储在数据库中可使值更易于维护。

I would have to agree @Gordon_Linoff. 我必须同意@Gordon_Linoff。 I Don't think you will end up with "way more tables", maybe one or two. 我认为您不会以“更多的方式”结束,可能只有一两个。 If you had a table that had fields of ID, Weapon, Min, Max ... Then you could do a recordset search when needed. 如果您的表具有ID,Weapon,Min,Max ...字段,则可以在需要时执行记录集搜索。 As you said, these variables might never change but changing them in a single spot, seems much more Admin-Friendly then scouring code that you have let alone for a long time. 正如您所说的那样,这些变量可能永远不会改变,而是在一个位置上进行更改,似乎比您长期以来一直精讲的精练代码更具管理员友好性。 My Two cents worth. 我的两分钱价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM