1

This is in context of a client-server architecture, although I don't think the architectural setting is needed to put up the problem.

I have a function whose output can be classified into different categories:

function makeCar() { /* make a car */ }

Now, in order to decide the structure for the return value of makeCar, I have several options:

Option 1

Return integer values and have a data structure that maps the integer values to different types of cars.

Example:

    codeToCar = {
                 "1": {
                       "model": "Jaguar XJS", 
                       "color": "Black", 
                       "year": "1975"
                      }, 
                 "2": {
                       "model": "Chevrolet Camaro", 
                       "color": "Blue", 
                       "year": "1966"
                      } 
                 ...
                }

In this case, the function makeCar would return the keys of codeToCar.

Option 2

Return the car object itself and do away with the integer codes.

Option 3

Return a hybrid structure with some integer codes and some specific values.

Example:

    codeToColor = {
                   "1": "Black", 
                   "2": "Blue", 
                   ...
                  } 

    codeToMaker = {
                   "1": "Jaguar", 
                   "2": "Chevrolet", 
                   ...
                  } 

In this case, the function makeCar could return

   {
    "color": 2, 
    "maker": 1, 
    "model": "Camaro" 
   }

What are the pros and cons of using the different options?

Edit:

I guess I should have elaborated the context more. So basically the function makeCar executes on a client browser and the return value of the function is passed to the server in an HTTP request. Any representation for the return type of makeCar on the client side would ultimately be translated to a full object representation (option 2) on the server.

The two parameters I chose for comparing between the different options are:

  1. Number of bytes transferred over HTTP
  2. Extensibility in case new properties are added to cars or new values are added to an existing car property.

I, however, would also be interested to know what other parameters could be used (in the current context) to compare the options available, alongwith choosing between the different options based on the chosen parameters.

  • All 3 options return different things. I presume the best option would be the one that actually does what the function name says.. make a car simply. If you wanted multiple cars to be returned, you'd return an array of them and call the function makeCars. – Neil Jul 15 '14 at 9:39
  • This example reminds me of how in the past I customized a Nike Shox on Nike website. Think about it as the client creates a shoe design with some property, and even though they are anemic items (with no behavior), when you change some property like heel color, the padding material might be changed, price might be changed, all sorts of things can happen. I don't quite remember if everything about the shoes is customizable from the user, or the server might actually add in more data and template to actually represent the full shoe. – InformedA Jul 15 '14 at 10:18
  • What would you do if this was just a normal app? You'd choose option 2. I'd go that route until you prove that to be wrong. From your examples, I don't see options 1 or 3 buying much for you unless this message(s) is sent at a high frequency rate. Are IDs really going to save enough bytes to make a difference for the added complexity and additional debug effort? – Dunk Jul 15 '14 at 14:14
  • This is one of those situations where you don't yet have the experience to understand the pros and cons of each approach and no matter what choice you make you end up making some compromises. Unfortunately, without that experience you can't know enough to know what's best. If it's important enough to the overall architecture I would try both options in different scenarios to see what the ramifications are. Only then would you know enough to decide and even then you'll have to compromise, but based on your priorities. – Mario T. Lanza Aug 14 '14 at 17:46
1

This isn't specific to return types, you're just asking how to represent a car. The answer depends on what properties of cars you need to represent, how those properties vary, and what you actually do with a car once you have it.

There is no absolute, perfect way to represent physical objects: you're always choosing some abstraction, and the choice depends on what you're trying to achieve. So, the answer would be different for a driving game, from a physics simulation, or an inventory or stock control system.

If you have very few possible cars, then an index into a hard-coded array might be fine. If your cars need to be modifiable (eg, changing the colour), an index is unlikely to be a good choice.

If your cars are just inventory items and don't have any behaviour, then a flat list of properties is probably fine. If they have to do something (like drive) then maybe each distinct model could be a subtype, but the colour could still be a (mutable) property.

0
  • Definitely not use #1 because then you need far too many codes to represent all possible configuration of cars

  • If you want to use 3, then you need to develop a standard to translate between the code and the actual naming strings. This would work well for many real word problems as there are already many well-established standards for things like colors, company ids, etc... This can also work when the users want more control and want to develop their own identification frame.

  • If you use 2, then you make things more free-form which would suit best for the case where the standards have not become well-established, many conflicting standards, you don't want to develop the standards, you don't want to user to develop the standard that would restrict them in the future. Especially fitting when the data is only used on the surface (such as display string) with minimal business logic depending on it.

In other words, choose 2 or 3. You need to specify your problem more. I would personally lean to ward 3 from what you have put here.

  • I have elaborated the context of the problem in the edit. – Bharat Khatri Jul 15 '14 at 12:26
  • Then use 3. Now that you have clarified I understand what option 2 really meant, I wouldn't use 2. In my experience, though limited, I have never used option 2 for anything. I always thought that 2 is the kind of options used by the framework when some low-level things need to be scaled into multiple servers, basically low-level protocol would use 2. I might be wrong. Nevertheless, pick 3. – InformedA Jul 15 '14 at 12:36
  • Uh ohh, my bad. It looks like you are talking about Javascipts interaction with your server, not your web app in Java communicating with a Java backend. I have no expertise in Javascript, ignore my answer. – InformedA Jul 15 '14 at 16:00

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.