4 ms·
Encapsulation isn't the real reason why types help. Pass 5 variables individually, the only difference will be in verbosity. The real benefit is type narrowing
by pcwelder 2y ago
Encapsulation isn't the real reason why types help. Pass 5 variables individually, the only difference will be in verbosity.
The real benefit is type narrowing. (Or declaring the space of all possible combinations of values.)
Instead of
id: null | string
name: null | string
...
You'd have
user: null | User
And
User: { id: string, name: string, ..}
declaring that all are not null together.
Pushing validation to static checker instead of runtime.
- afc 2y agoAgree. I try hard to push the validation to static checks, but it can't always be done. For example: - a "probability" represented as a double must always be between 0 and 1 - A vector of values given to some group of functions must always be sorted (by some given order) But even in those cases, using types allows me to ensure that this validation always happens (when the type instance is created). It also let's me avoid having to explicitly validate this inefficiently (e.g. redundantly in preconditions in functions that receive these values). This doesn't change the fact that static validation is a better approach, but compliments it. I wrote a bit about it here: https://github.com/alefore/weblog/blob/master/edge/correctness.md#ghost-types-validators https://github.com/alefore/weblog/blob/master/edge/correctne...
- default-kramer 2y ago> Pass 5 variables individually, the only difference will be in verbosity. Which can be an extremely significant difference. If you're already using a single object and add a 6th field, you only need to update the places where the object is constructed and where the new field is consumed. If you're using individual variables, you also need to update all the code that those 5, now 6, variables simply pass through.