Steven Sokulski
Steven Sokulski

Reputation: 374

Better to parse an array or add a database call?

I'll be coding up both to test them, but I was curious on first blush what the folks here thought.

I've got an array of data that has been posted to my shopping cart controller. The array includes an entry for each of the product's potential options.

To get the options into an array for presentation I can either parse the array looking for arrays with keys that begin with "options-" or I can make a database call and find out what options are available for that product.

We are talking about at most 5 items at this time.

Thoughts?

Upvotes: 1

Views: 107

Answers (2)

user1003932
user1003932

Reputation:

Pros of Hard coding

  1. More readable code
  2. Less DB use. Though you could use memcached to store a key:value part of options.

Cons of Hard Coding versus DB call

  1. Typos will bring down the cart.
  2. If you need to reference the options in several locations, you will make errors

Upvotes: 0

Brad
Brad

Reputation: 163603

I think the keyword here is that the data is POST-ed to your controller.

You should never trust user data. Always verify user data with real data. So, if a user adds something to a shopping cart, make sure you go back to your database and ensure that what was added really does exist. Since you will be making the query at that time, best to rely on the data from your database.

Otherwise, data you already have in memory is certainly faster than going to a DB. Typically, you want to avoid making additional queries that are not needed.

Upvotes: 3

Related Questions