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.
4
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.
-
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.
3