Comparison of operator overloading abuse in different languages

coding-styleoperators

A common argument against operator overloading is that it can be and is abused, e.g. A+B doing something completely different to addition. Examples are often quoted in C++ snippets, where the functions overloading the operators don't name the operators (e.g. operator+()). Compare this to Python, where the operators are named (e.g. __add__).

Does this help to deter programmers from abusing overloading in the aforementioned manner? Anyone have any experiences of Python operator overloading being abused?

Best Answer

Does this help to deter programmers from abusing overloading 
in the aforementioned manner?

Well, I think first we should define what the abuse is? File.Delete in C# simply deletes a file without even notifying the user about it. Thus, as a developer, I can simply write this code:

List<string> files = Directory.GetFile("path").ToList();
files.ForEach(f => {
    File.Delete(f);
});

Is it considered abuse? OK, you're free to define + symbol (character) as an operator which votes up and - as a vote-down operator. But is it considered abuse?

Also, are you forced to misuse these features? For example, a driver can simply turn the wheel to get to pedestrian area and kill may people? Should we lock the wheel?

I mean, after all, even if there is an opportunity for any kind of abuse, who's gonna get hurt? Developer.

In spite of highly OO infrastructure, you can write procedurally in C#. Is it abuse?

Related Topic