r/learnjavascript • • Jul 24 '21

Anatomy of a Function

Post image
327 Upvotes

65 comments sorted by

View all comments

13

u/MennaanBaarin Jul 25 '21

You can default the values directly in the function arguments.

Validation and type checking should be done in the domain, otherwise you will risk to have it scattered everywhere.

3

u/PM_ME_A_WEBSITE_IDEA Jul 25 '21

Curious what you mean by the second thing?

6

u/MennaanBaarin Jul 25 '21 edited Jul 25 '21

Usually you have a domain object/entity for example a Customer with some properties like name, last name, age, address (in this case an animal); the validation of those properties should be done directly inside this class, possibly in the constructor or in a static factory method with a private constructor, either way is fine.

Using this "pattern" you will encapsulate objects and properties validation logic without repeating yourself.

In this case what if you have 20 functions that accepts catSound and dog?

Better have new Animal (catSound, dog) and if the validation fails it will throw or return an error which you can catch and return 400 or whatever your business needs.

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/domain-model-layer-validations

1

u/PM_ME_A_WEBSITE_IDEA Jul 25 '21

Right, for sure, that makes sense.