3 ms·
I honestly don't think "private gettable String fname" is in any material way better than getters and setters, as your ide can already insert the boilerplate fo
by Tuna-Fish 17y ago
I honestly don't think "private gettable String fname" is in any material way better than getters and setters, as your ide can already insert the boilerplate for you. The problem isn't the boilerplate per se, it's forcing you to think about things before their time. Your attention is a scarce resource. The only satisfactory way for the first version is "public String fname", with the ability to later turn that into proper getters and setters when it is needed without the consumers of the class even knowing.
My big point about python is that you can always inject the dependencies, and you don't have to spend any thought beforehand to achieve this. Java could go a long way here with just a few minor changes -- for example, remove "new", you get a new object by calling it's constructor, without any special operators. Allow shadowing of functions. This alone would end the factory madness -- in fact, factories are just an awful boilerplatey way to implement this. For example, if I have a class Foo that needs to instantiate Bar to function, and I want to inject a different bar for some tests (or something), in python I can just:
def make_test_foo():
Bar = DummyObjectConstructor
return Foo()
(this will of course occur only in local scope, so that other potential users in other threads are not going to be stuck with dummy objects against their will)
To achieve the same in java, the way I was taught to was to design in a BarFactory for use when needed. In most cases, it's not going to be needed. But if you don't make it, boy are you going to be in a world of pain when you do need it, and half the world already depends on your class.
What really irks me about this, is that hotspot is perfectly capable of dealing with these kinds of things -- it is a really nice platform for this kind of dynamicity. Sun just used think that poor programmers are going to be confused when you hit them with too many high-level concepts, and refuses to add constructs like this. Which would be fine, except that the programmers invariably go on to develop their own ugly workarounds, like factories, because they need the power.
This I believe to be the big reason why python is so much faster to program in than java. I can program for the now, completely ignoring superfluous architecture when my programs consists of 3 files, with the knowledge that I can safely add it later when needed. The syntax and dynamic typing are just a nice extra.