Cost of using exceptions with PHP and Valueobjects

Asked

Viewed 66 times

2

I have always heard that exceptions have a high processing cost and make the application slow.

In the specific case of PHP, how much can we abuse the use of exceptions?

In the case of data validation in Value Objects for example, does it make sense to use throw to throw exceptions when the values received do not satisfy the business rule? Or would it be better to implement a list of violations? I say this in terms of performance.

Thank you in advance.

2 answers

4

This is worth much more for languages that care about performance, is not the case of PHP. If you want performance look for another language.

We should never abuse exceptions for semantic reasons, not because of performance. If the situation is not exceptional, that is, if it is not an exception to what normally happens, it is quite obvious, but extremely ignored by most programmers, some even with arguments, that the exception mechanism should not be used, it would become a normal flow control.

Validation should never generate an exception because the name itself is saying, you are wondering whether it is valid or not, being invalid is not an exception, it is something that is expected to happen.

However I know that many DDD implementations encourage the use of exceptions in this case, one of the reasons I abhor DDD. Preaching the wrong semantic use already shows how the technique is not good. But I also find it very strange to use DDD in PHP.

I would adopt a validation mechanism and not an exception to validate, regardless of the question of performance in any language and any validation situation.

Nor will I consider that people capture exception in the wrong way almost always, because I do not think that something should stop being used because people do not use right, although this argument has a certain strength when it comes to exception.

Related questions:

2


If you are using in Exception for what he should be using, ie exceptions, have no reason to worry about perfomance. The idea is to treat the code as much as possible to avoid errors (testing null objects, for example) and, if something unexpected or not mitigated happens, then fire a Exception.

In the case of your question, you are thinking of the scenario of validation of a business object and firing a Exception if it is not valid. This @Maniero already put well in his answer, if it is a validation rule, it should not fire a Exception, and if so, you should create a custom Exception for each type of validation.

There is no point in firing an Exception and the caller knowing that "something went wrong in the validation"!
Ah, but you have a message, something like "invalid number," but let’s evaluate message? It doesn’t look good, especially since the messages can change... should trigger a custom exception, a class that extends Exception and shoot, something like:

class ClienteCPFInvalidoExceptionextends Exception { ... }


if (!CPFValido($cliente)) {
   throw ClienteCPFInvalidoException();
}

And then use a catch specific:

catch(ClienteCPFInvalidoException$e){ ... }

But then I would have to create a type for each validation, so that whoever runs the code can "take" the correct Exception and know how to handle, but then, how much code would have to implement?

Maybe it’s more practical in this case of your question, as you yourself mentioned a list of violations and not worry about the cost of a Exception

Browser other questions tagged

You are not signed in. Login or sign up in order to post.