Is TimeSpan unnecessary?

后端 未结 9 1948
野的像风
野的像风 2020-12-15 06:33

EDIT 2009-Nov-04

OK, so it\'s been a little while since I first posted this question. It seems to me that many of the initial responders failed to r

9条回答
  •  隐瞒了意图╮
    2020-12-15 06:56

    Just because you can define an operation doesn't mean you should. For example, one of the reasons division by zero is undefined is because defining it would require sacrificing some very useful properties of arithmetic (eg. associativity, etc).

    The distinction between a timespan and a date comes down to addition. It makes sense to add two timespans, but it doesn't make sense to add two dates unless you have an arbitrary reference date. By not allowing addition of dates, you abstract away that arbitrary reference date. I don't know what date '0' is in .Net, and I've never needed to know. Isn't that nice?

    Adding two dates is almost always a bug (seriously, try to think of where this makes sense outside of numerology). By introducing timespans (creating an Affine Space) you eliminate a whole class of bugs.

提交回复
热议问题