Best design pattern for checking data / multiple conditions

Hi, what is the best design pattern to use for a data validation scenario, when many values ​​are passed to the function, and all should be checked for the correct format and length, etc. Im currently uses a few if statements, but it seems very dirty.

+4
source share
3 answers

Take a look at the spec template. It fits into Domain Driven Design and has a fairly simple concept.

Hope this helps.

Brendan

+1
source

Well, it depends on the current state of your code.

I will try to follow this:

1) try to extract the same confirmation code and, possibly, pack it as separate functions / modules, etc., just call the function for verification, do not put the verification code in the validator

2) with this you can create a master validator as an integral part of validators, sub-steps for validation validation in action classes and build a large validator that will launch all these small steps, and will not work if one of the subtasks fails (but first, evaluate, if this is not redundant in your case, most likely it will just end the refactoring here if your check is not so complicated)

3), then given this pattern, if your validation logic changes frequently, you can try to put the configuration of the composite code outside the code. If you have access to control inversion tools, such as the Spring Framework, you can create a composite from an XML configuration, not from code. This has the advantage, since your change requires verification, you do not need to rebuild the project to make changes and separate the business logic from the code.

+1
source

You can try the development strategy template

0
source

Source: https://habr.com/ru/post/1315642/


All Articles