The task is this: there are certain string representations of the spec. numbers. It is necessary to make a class with methods bool IsCorrect (string number) SomeObject Parse (string number) string Extract (string someString) bool Contains (string someString)

The class is auxiliary, and is located inside the assembly.

The question is what should the methods do in case of an incorrect argument? For example, if the IsCorrect method accepts null, should it check the input parameter for null or is it implied that class users should take care of this themselves? class internal? Or else such a situation, Parse / Extract methods accepted an incorrect parameter (the number does not match the format), which the method should then do, check the input parameter for correctness, and throw the special. Is there an exception if the number is incorrect or the class user must check the transmitted number for correctness in advance?

  • With checkers that return bool, everything is simple and logical - the truth is only with the correct value, the rest is false. null is the same wrong value as others. Regarding the parsers, I agree with @KoVadim, but you can still see how the parsing is done in the .net itself, for example Guid-s, which are quite a special kind of numbers. There are simply 2 methods: Parse, which drops with an exception for any invalid value, including null, and TryParse, which drives checkers, and if everything is fine, the parsit. - rdorn

1 answer 1

The questions that are mentioned in the question are very philosophical and all solve them in their own way.

There are ways.

  1. Do not bathe. How to do it / how easy. No comments.
  2. How to do it , but describe the behavior in the documentation. That is, write explicitly - the Parse method should receive a non-empty string and at the same time a valid one. Otherwise, the behavior is undefined.
  3. Lay the straw. My favorite. The bottom line is that we do a lot of different functions / methods that can check input parameters. Now, in debug mode, these methods are always called for all input parameters of each method. In the release, it all turns off. To do this, even have a wonderful thing - Assert . But I often make my own macro for this, which is controlled from the config / command line. In this case, if something went wrong, you can enable checks and catch the right place at the customer. Such a unit tests on a live customer. Of course, this functionality actively writes all checks to the log (even the correct ones and writes all the parameters).