How is the "high up" parameter checked in the freeze frame?

I read the book "Framework Design Guides", a book on developing frameworks in .NET, with excerpts from the framework designers about the decisions they made regarding each section (for example, designing parameters, handling exceptions, etc.).

One piece of advice when designing parameters is to check the parameters as high on the stack as possible. This is due to the fact that the work here is not as expensive as low at the stop cash desk, so the performance penalty is not so expensive when checking high in the stop lot.

Does this mean that when I pass parameters to a method or constructor, I check them before doing anything else, or do it just before using the parameters (Thus, there may be 100 lines of code between the parameter in the definition and the use of the parameter )?

thanks

+4
source share
5 answers

I have not read the specific recommendations you mentioned, but I expect them to talk about the case when Method A calls Method B , which calls Method C , and the parameter value is passed through all three calls. It is better to check this parameter at the beginning of method A than somewhere in the middle of method C , because if it is not valid, then you can skip everything that happens in A and B and the beginning of C This is especially true if B or C cause inner loops, because then a low-level check will occur many times, and not just once at the start of A

Of course, you need to balance this with how difficult the parameter check is. It may be easier to understand if you confirm it in the same place where you use it.

+1
source
  • Prefer to claim in the public assembly API. This means publicly available public practice methods.

  • Prefer to check the public methods of your classes. Therefore, if your class requires a non-null pointer to another object to work correctly, you could force this by requiring it as a constructor parameter and throwing an exception when specifying a null pointer. From now on, none of the member methods should check to see if a pointer is specified.

The idea is that no user can break your class (or assembly) by submitting incorrect data. Of course, the code will not work in any case, but if you fail in a controlled way, the more understandable for the calling code that is not so, and you will not have unpleasant side effects, such as resource leaks (or, even worse).

+4
source

Failing fast is usually good practice. All arguments passed to the method should be checked as soon as possible, without any unnecessary calculations performed earlier, as this facilitates debugging and simplifies recovery due to a malfunction.

Regarding input validation, I think performance is a minor issue.

+2
source

Confirm them as early as possible in your method!

+1
source

I believe this means that you should check for data that may be invalid once you receive it. Once it is verified, no further checks will be required. If you wait until the bottom of the call stack, you will have to check many times because the call tree can have many branches.

I would rightly agree with this advice, but not for performance reasons. Confirming at the entry point, you are in a much better position to give a meaningful error message to the client who provided the data. And by reducing the number of checks you do, you will get a much clearer code.

+1
source

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


All Articles