I was researching the execution time difference between static and instance method calls today and came across an old Java forum thread about the topic. I was looking for performance information, but everyone in the thread was focusing on “good <abbr title=”object oriented>OO</a> practices”, and I was astonished as to what I was reading. Because of this thread I realized that many “patterns” are created as “good OO practices” because of a flaw or limitation in a language. Then that pattern is said to be correct and good and carried over to any language regardless of whether it has the original language flaw or not.
In this thread they were discussing how you should always use instantiated objects because you will never know what you might need to change the code for later. This threw up two flags in my head:
- Why do static methods/classes exist in Java if they aren’t advantages to use in certain situations.
- These people have obviously not looked at any dynamic languages like Ruby, Python, Perl, etc…
As for the second point, I will let them slide because the thread was from around 2005, and the dynamic languages were just starting to pick up steam. The first one I can let slide because I understand that even language engineers make mistakes, and could have possibly written a feature that isn’t necessary or good to use (e.g. goto).
But that still leaves me with the problem that this is a language issue not a good OO practice. Java does not allow you to change the behavior of a static method at runtime, or even override it at compile time, but in a language like Ruby you don’t have this limitation. Keep in mind in Ruby you have Class methods not Static methods, but it is basically the same idea. Furthermore you can override any method, class or instance, at runtime in Ruby.
This makes me wonder how many other “good OO practices” have been brought from old languages unnecessarily. I know when I first started writing ActionScript or even C# I fell prey to writing getter and setter methods. This is completely unnecessary but I am sure an experienced Java developer would try to convince you to do it as a good practice. I would imagine even if you convinced him that [insert language here] has the notion of formal properties they would attempt to convince you to do it to make the code more readable, but it is what you would expect to happen.
This really bothers me because if this were the mindset of the original language developers we would probably never have a for loop because a while loop works just fine. That may be a bit of a stretch but I think it is the same mentality. Don’t use the new syntax/feature because the old way is more familiar. If we were to do that, what is the point in writing new languages, or improving old ones. I guess I shouldn’t be surprised that many people fall into this mentality because the programming community up until the last few years had seemed to fall into a rut with language development. “Why develop new language constructs while the old ones work?”.
I guess overall this just further encourages me to learn a new language every once in awhile to make sure I am not falling into this mindset, and make sure I don’t try to make the new language bend to my ideas, but rather bend my ideas to the language. I guess I would equate it to speaking Spanish but not putting your adjective after the noun because that is how we do it in English. I charge you, learn a new language, swap that adjective around. It will probably make you a better developer by giving you a different slant on the problem.