ssougnez
ssougnez

Reputation: 5876

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.

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.

Upvotes: 0

Views: 421

Answers (2)

Matthew Cochrane
Matthew Cochrane

Reputation: 56

I would have to agree @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. 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.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269953

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

Upvotes: 1

Related Questions