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:
- Number of bytes transferred over HTTP
- 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.
makeCars
.