Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hardcoding Arrays and Objects or populate from Database? [closed]

At the present moment I am putting together a product website for my company featuring about 80 or 90 unique products with various characteristics.

Initially, I hard-coded a multidimensional array to store the products and their various characteristics. Aside from being a lot of typing to set up it also felt like it would be challenging to update/maintain.

I also felt like I had a lot of spaghetti code elsewhere on the site, so I learned about OOP and refactored the site utilizing class structures and interfaces. Very nice and clean. Except, now I am faced with hard-coding objects, of which there are many.

I've done a lot of reading and know I can probably convert my array data into objects, but nonetheless the issue of maintenance keeps cropping up as some product characteristics may change frequently. My solution to this is to use a mysql database (which seems easier to maintain) and extract the product and characteristic data from there into objects.

My question is:

Is this typically best practice? Are arrays/objects typically populated this way and it was crazy to hardcode it all in the first place?

Apologize in advance for the newbie question. I have done a substantial amount of reading but haven't come across anything that gives the direct advice I'm looking for. My brain might be short-circuiting but I have not been able to synthesize it clearly from what I've read either.

like image 351
mjs Avatar asked Jul 12 '26 10:07

mjs


2 Answers

If the only query you're ever going to run is SELECT * FROM `table`;, then you could use a hardcoded array. If you will be using ANY other query to access the data, then it should be in a database.

like image 85
Niet the Dark Absol Avatar answered Jul 14 '26 23:07

Niet the Dark Absol


In your case I would definitely refactor the existing code and put it on the database rails.

For refactoring purposes I suggest you to use model objects which can be populated either from database or from your hard-coded arrays using different adapters.

Rewrite your code with adding model objects to work with your business logic entities using existing data source (hard-coded array); and then change data source to the database.

like image 23
zavg Avatar answered Jul 15 '26 01:07

zavg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!