1

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?

2
  • If you already have the data there is no need to make an extra database call. Commented Sep 16, 2012 at 23:41
  • parsed array will be faster, but its unlikely to make any real world difference Commented Sep 16, 2012 at 23:42

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.