-
I can define constants in file and then include them, or
-
I can store them in DB and seed them.
From your experience, which one is better/faster/less resource costly for say .. dozens, hundreds, thousands defined?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is an ever-present question, which differs from person to person, and is a bit of a balancing act. For me, it all depends on how big and static the data-set is presumed to be. In my applications I have about 10 code points where I will refer to constants rather than going through all the overhead to pull data from a database.
Another benefit of using constants, is you can refer to them throughout the codebase which will help reduce defects and improve semantics, for example:
However there are a few hmmm downsides? I hesitate to call them downsides because they are not difficult to work around. First, if your constants are ints, and your application ever inputs these contants into a table, you will not be able to join to a ‘lookup’ table and see the lookup values as part of the query results. Instead you will just have to know what an id of
1means in the query.Second, be sure to place constants in common sense locations, and try to namespace if you can, that way you can easily find the values if you end up forgetting what they are. If you have a clever IDE, it will do this for you. Also know that if a constant changes, you will have to do a code push vs manipulating database data directly.
I’d say yes to constants, if the list is relatively small (<25) and not prone to change much, however, if your data won’t match these constraints, I would suggest popping them into a database.