4 ms·
what are some specific disadvantages of operator overloading in the instance of subtracting or adding months together?
by codygman 11y ago
what are some specific disadvantages of operator overloading in the instance of subtracting or adding months together?
- agentultra 11y agoIt's actually a pretty bad example, apologies. Python ships with a standard library that includes modules such as datetime, timedelta, and calendar. I've been guilty of using all of them. I can say that when your date math becomes complex enough you might want to consider avoiding them and doing the calculations yourself. In a recent project I had to integrate several systems that stored metering data in UTC with various databases in different local time zones for the purposes of billing on that data. I can say that doing the math is easier and more clear than using objects that encapsulate the data in such complex scenarios. There is no hard-and-fast rule for me to determine when operator-overloading is inappropriate. I only have a few "smells" I use that raise the hairs on the back of my neck when I encounter them: 1. If your object has one or more boolean members 2. If your operator has to dispatch on more than 1 type 3. If your operator breaks any of the associated identities Any of these are just warnings. In the case of matrix addition it breaks the Commutative identity of addition but is a well-defined and known property of the operator for anyone who would be reading code involving those kinds of objects that it can be justified. Dates might be defined-enough but intuition clearly get the best of the OP. It's just a trade-off that I haven't found valuable outside of certain special cases.