2

This question has been asked first on Stack Overflow, I've been told that this forum would be more appropriate

I am wondering what's the recommended way to solve the following situation :

  • I have items, Cars which have a color and a car model property
  • When a customer choses a car on the carmaker website (the one I am in charge of), an image is sent back to his browser for visualization
  • So, I logically have a database which stores an image file path for every brand/color combination, that I query any time a request is made.

Right now, the carmaker wants to implement additional color choices, which are really close to existing ones, let's say crimson and firebrick in addition to an existing darkred. For visualization purposes, we use the image of darkred for the three colors.

So, here is my question, I can think of two ways of implementing this change :

  • Either I hardcode into the ImageManager class (or its config object), the class in charge of retrieving images from the database, that darkred, firebrick, and crimson colors are all refering to darkred when queried
  • Either, I keep everything as it is and I just append new rows into the existing datatable pointing all new colors/car model combinations to an existing image (this is a lot of rows).

From your point of view, what'd be the best design choice and why? I am wondering about coupling, future evolutions and maintenance.

4

The answer to such a question is, it depends.

You need to ask yourself a series of questions:

  1. How often are new colours added to the models?
  2. Is the whole range of cars affected, or do some colours only exist for some models?
  3. How easy is it to modify, retest and rerelease the software?
  4. How easy is it to modify the database?

Depending on the answers, it may be quicker, simpler and more reliable to modify the code. Or it may be simpler to modify the database. Go with whichever one is quicker, simpler and more reliable. And if it's not clear cut (one is simpler, one is more reliable for example), go with the more reliable option. Saving a few days work counts for little if you make a mistake and serve up an image of a blue Vroom car, rather than a red RevRev car for example.

There is a third option though that may simplify things for you. Rather than having entries for all three colours for all models in one table, introduce a new colour mapping table. So it would map crimson, firebrick and darkred all to darkred. That way, you only need an entry for the latter in the image mapping table. Of course, the risk with this is that if the car maker then supplies you with an image of a firebrick Vroom, but not one for the RevRev, you are stuck and potentially then need to search on the colour in the image table, then use the mapping table if not found.

As with any problem like this, there are multiple solutions, each with pros and cons. Don't assume any of them is the right solution. Weigh up the pros and cons and pick the one that best fits your particular set of circumstances.

2
  • Awesome, thanks for this detailed answer. – XavierAM Oct 15 '19 at 8:48
  • 2
    One thing you should also consider is that the prospective customer should be made aware that the sample image shows a car with a very similar but not exactly equal color. This is easier if you have an explicit mapping table and your code knows whether it retrieved a perfectly matching image or a close one by virtue of the mapping table. – Hans-Martin Mosner Oct 15 '19 at 13:52
1

Similarly to David, I would simply add a colors_fallback table.

This would be filled with mappings like crimson -> darkred.

Then check if there is an image with this car model/color, and if not, use the fallback color.

You can even do this recursively ;) If model/darkred isn't there either, the colors_fallback might for example contain darkred -> red and red -> default. That way the model/default image would be taken in a last resort in case the other colors are missing too for that brand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.