Create Data Type
For more complex and growing systems, it is more readable and maintainable to create type instead of using too many parameters or maps.
There is one exception. The exception is when we really need to handle dynamically changing number of parameters.
Too many parameters
As the code grows, we can end up with methods that have too many parameters.
class UserService {
public void save(int id,
String fisrtname,
String lastname,
String street,
String city,
String apt,
String ssn,
boolean active,
boolean update) {
// for example, it gets saved into database
}
}When we have so many parameters, we want to create an envelop data types that would wrap these objects. The easiest solution is to use a map, because it is provided by language itself. Lets see what is wrong with that.
Using Maps everywhere instead of classes
Lets look at an example. The controller gets some parameters. We have got only three and we could just pass them into service, imagine there 20 parameters that needs to be wrapped before they are sent to service.
It is questionable if using maps everywhere is wrong for dynamically typed languages. It depends, but many times, it is better to have data type for better maintainability.
What is wrong with using maps:
We are loosing information about type
It is not clear what is the signature of the method, maps are too generic
Keys in the maps are all around the code and it is difficult to do refactoring (we can create helper class to store the keys, but it is just creating another class which shouldn't be even created)
Using Data Type instead of Maps or too many parameters
Lets create data types instead of using the maps or instead of having so many parameters.
Last updated
Was this helpful?