C # Programming Practice – Maternity leave Program Optimization edition

Source: Internet
Author: User

Objective

Since as a solid learning technology, there will be a humble, open-minded and the pursuit of excellence in the heart, I can not write a very perfect code at once, but I believe that the step-by-step optimization, the code can become nearly perfect, at least in a particular scenario under the relatively perfect, this and a person has a very similar truth, This is an attitude. A few days ago, I sent my first coding practice in the blog Park, "C # Programming practices – help wives calculate maternity leave scheme" (referred to as the previous article), attracted a few Bo friends (this is a few of them have worked together for several years of classmates and colleagues, feeling that everyone in the technical road on the insistence and not easy) of the onlookers, they Of course they also made suggestions, I do not intend to ignore these suggestions, so there is this article.

Complex or simple?

Complex and simple, at some point is a relative concept. In the previous article, some friends suggested that my code was somewhat complicated. I have a humble attitude to analyze, I must admit, I write the code is too much design suspicion, violates the K.I.S.S principle. My work experience has led me to write code in the mindset of medium and large projects, considering object-oriented design, considering some of the metrics and specifications of the architecture system (high availability, stability, readability, maintainability, extensibility, testability, etc.), so some friends say my code is too complex, I think I can be more concise to outline my ideas, let everyone know my original intention.

    • Holiday definition

Duties: the expression and storage structure of festivals

Properties: Use month, day, and calendar to represent festivals

Static read-only field: represents immutable constants instead of magical variables (magic number)

Design Reference: Reference the datetime structure definition of the. NET Framework BCL

    • Holiday definition

Responsibilities: Holiday expression and storage structure

Properties: Holidays, Holiday rules (days in advance, a total of days off)

Logic: Turning holidays into a common enumeration set (ienumerable<datetime>)

    • Logical rules

Determine whether the rule logic of the vacation, the interface is as follows

   1:publicstaticbool isholiday ( this DateTime date);
   2:publicstaticbool isholiday ( this DateTime date, Ienumerable 
    
  
    • Conclusion

So far, I think, we think that the so-called complexity lies in two points:

  1. The storage and presentation of data, "I go through the calendar and use a collection to store all the holiday dates. ”
  2. Logical dependency, Isholiday logic uses extension methods to represent and holidays dependent, in fact, there can be some optimization, such as:
   1:publicstaticbool isholiday ( This DateTime date, func< Datetime,boolean> holidaycondition);

Using delegates, refer to Ienumerable<t> 's LINQ extension method design, which relies on delegates to implement specific logic for the caller

Optimization

As I mentioned in the previous article, LINQ queries can be optimized, and my query is obviously bad, do you know that there are two iterations to choose between 98-day intervals from a single time period? Oh, my God, do you remember? This is the order of the Cartesian product, we have no need to do selectmany operation Ah right, use an iteration can be done to solve the query why use two times? Right, the following are the optimized LINQ queries:

   1:new DateTime (2014, 1, 1);
   2:new DateTime (2015, 12, 31);
   3: var sample = Dateutility.rangeday (begin, end);
   4:
   5: var solutions =
   6:      in Sample
   7:let last     = iterator. AddDays (days-1)
   8:let     range = Dateutility.rangeday (iterator, last)
   9:     where last <= end
  Ten:     New
One   :     {
  :         Begin = iterator,
  :         End = Last,
  :         Holidaycount = range. Count (d = d.isholiday ())
  :     };
  16:
  : var local = solutions. ToList ();
Conclusion

Writing code is simple, and writing good code is hard. Originally wanted to write a simplified version of (because everyone feels too complicated), but the time is limited, first here!

C # Programming Practice – Maternity leave Program Optimization edition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.