Which design is most preferable: test-create, try-create, create-catch?

前端 未结 4 1787
野的像风
野的像风 2021-01-30 16:20

Let\'s assume there is an operation that creates a user. This operation may fail if specified email or username exists. If it has failed, it is required to know exactly why. The

4条回答
  •  被撕碎了的回忆
    2021-01-30 16:38

    This is somewhat subjective, but there are some concrete pros and cons that are worth pointing out.

    One drawback of the test-create approach is the race condition. If two clients attempt to create the same user at approximately the same time, it may be possible for them to both pass the tests, and then attempt to create same user.

    Between Try-Create and Create-Catch, I prefer Create-Catch, but that's personal taste. One could argue that Create-Catch uses exceptions for flow control, which is usually frowned upon. On the other hand, Try-Create requires a somewhat awkward output parameter, which could be more easily overlooked.

    So, I prefer Create-Catch, but there's definitely room for debate here.

提交回复
热议问题