I've mentioned before the Principle of Least Astonishment where the software shouldn't do things that would surprise the programmer. Another example of this principle sometimes comes up when developers try to implement the Singleton pattern. A Singleton is guaranteed to have only one instance. Some developers choose to accomplish this by overriding the class new method to return the single instance. On the surface, this seems like a reasonable approach. The problem is that it breaks your assumptions when you read the code. The normal expectation when you see code that says MyClass new is that you're receiving a new instance of the class. If you override new, then you are actually receiving the same instance each time. This breaks the developer's normal assumptions about how things work. If you want to implement a Singleton, have a method called singleInstance or current or default but don't override new. If anything, override it to raise an error to indicate that this isn't a valid operation. If you use new for the singleton, people reading your code will likely misunderstand it which will lead to problems.
Download