Home  >  Article  >  Backend Development  >  Through Parse and TryParse Try-Parse and Tester-Doer modes

Through Parse and TryParse Try-Parse and Tester-Doer modes

黄舟
黄舟Original
2017-03-04 10:36:381482browse

Parse and TryParse

In DateTime, Parse(string s) and TryParse(string s, out datetime) are used to convert character date and time into equivalent System.DateTime. So, is there any difference between them, except for the different parameters of the function. Let’s take a look at the code first:

            string dateTimeStr = "";
            DateTime dt = DateTime.Parse(dateTimeStr);

Run the empty string and convert it into a date and time type. Obviously it cannot be converted, and Parse() will throw an exception: System.FormatException: s is not included Valid string representation of date and time. However, running the TryParse conversion method:

            string dateTimeStr = "";       
            DateTime dt2; //dt2未经初始化,就被传递给函数TryParse()
            bool sucflag = DateTime.TryParse(dateTimeStr, out dt2);

 The conversion does not throw an exception first, dt2 is assigned the minimum value of the date and time, and sucflag is false. Take a look at the comments on the function:

When this method returns, if the conversion is successful, it contains the System.DateTime value equivalent to the date and time contained in s; if the conversion fails, it is System.DateTime.MinValue. If the s parameter is null, is an empty string (""), or does not contain a valid string representation of the date and time, the conversion fails. *This parameter is passed without initialization. This function will not throw any exceptions.

Try-Parse

After seeing their differences, further speaking, exceptions thrown by parse() will inevitably affect performance. TryParse() did not throw any exceptions. This is A design pattern that optimizes exception performance called Try-Parse Pattern. The following is Microsoft's official explanation:

For extremely performance-sensitive APIs, an even faster pattern than the Tester-Doer Pattern described in the previous section should be used. The pattern calls for adjusting the member name to make a well-defined test case a part of the member semantics. For example, DateTime defines a Parse method that throws an exception if parsing of a string fails. It also defines a corresponding TryParse method that attempts to parse, but returns false if parsing is unsuccessful and returns the result of a successful parsing using an out parameter.

Tester-Doer

When explaining the Try-Parse mode, Microsoft proposed Another mode: Tester-Doer mode, what is Tester-Doer mode? Writing exceptions in functions will reduce performance. Microsoft has provided this mode to reduce the side effects of exceptions.

The following code:

ICollection<int> numbers = 省略获取数据的逻辑
numbers.Add(1);//Add此处不做可写性检查

The above defect: If the collection is read-only, the Add method will throw an exception. Exceptions will often be thrown where this method is called, thus affecting system performance. In order to avoid this design flaw, Microsoft proposed: Sometimes performance of an exception-throwing member can be improved by breaking the member into two.

 Decompose Add() into:

ICollection<int> numbers = 省略获取数据的逻辑if(!numbers.IsReadOnly) //Tester{
    numbers.Add(1); //Doer}

 Tester-Doer Pattern summary:

The member used to test a condition, which in our example is the property IsReadOnly, is referred to as the tester. The member used to perform a potentially throwing operation, the Add method in our example, is referred to as the doer.

After decomposition, read-only detection is done first, which will reduce Add the number of times read-only exceptions are thrown to improve performance.

Summary
Try-Parse Pattern and Tester-Doer mode are two optimization methods that replace throwing exceptions and play a role in optimizing design performance.

The above is the content of Try-Parse and Tester-Doer mode through Parse and TryParse. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn