Beyond Java “Hello World”
So I slapped it in gear and started coding on the previously mentioned file update utility that I need to write. Starting small (as I referenced before). I decided to focus first on the lowest level of the program — copying a file. So I now have a command-line utility (with a problem or two) that can copy a file.
So what’s the big deal? Why am I blogging about this? Because I already have observed several things about Java (and Eclipse) that I wanted to point out.
It’s amazing how simple it is (in some ways) to transition to using Eclipse and Java from C# and VS.NET. Creating classes and writing some code is pretty straight forward. However, I was shocked when I discovered several things. First, I was amazed that it wasn’t until the 1.5 (or 5.0 depending on how you look at it) version of Java that it supported Enumerated data types properly. So immediately, I have decided to switch to doing a 1.5 (only) program. This will also allow me to use Java’s new for-each code as well. Second, and more importantly I was shocked to see how Java’s getters and setters were done.
I should point out first, that in general, I find most things (properties/attributes) do not require a getter or setter. As you move into a more UI widget oriented situation that starts to change, but for the most part, 90% of my class properties do nothing more than set or return a value with no interaction based on that. So having all these wrapper methods has always seemed a little excessive.
In C#, the cool thing about the getters and setters syntax is that you keep perfect syntactic compatibility interfacing with the class whether you have true getters and setters or whether you just have publicly available attributes. So you can switch as you need to. However, you do lose entry point compatibility… you have to do a recompile and you can lose some things (especially in the IDE) because of changes.
Python’s property syntax (which for some strange reason I was not familiar with) is actually like the C#.NET stuff. Same name and everything and the cool part is that because of its dynamic nature, you keep compatibility at the interface level. Very slick. I just wish that it had been used a bunch in wxPython.
Based on the other languages available, I find it amazing to hear the Java folks trying to defend their dorking version of getters and setters. As soon as you compare them to anything else they seem to rather silly.
More observations to come I am sure…
2 comments2 Comments so far
Leave a reply
For those of us that aren’t familiar, what is so bad about Java’s version of getters/setters? Is it just that you have to write your accessor methods manually? (I seem to vaguely recall that C# has some sort of automatic accessor methods that get defined when you add a property?)
I’ve written plenty of explicit getters/setters in Python. One advantage is that making them all explicit forces me to reduce them to a minimum set, since I hate typing.
I don’t know about “automatic” accessor methods in C#. There is the special “Set” and “Get” definitions and syntax for defining them. And I am sure either VS.NET 2003 or 2005 has something that will generate stubbed versions of those for you if you click on something.
Eclipse does have a getter/setter generator thing that seems pretty slick. But of course, the problem I have with it all is that if something is really a property/attribute I want to access it as such. It’s not a killer, but once you are used to myObject.x or myObject.width. It seems silly to have to a myObject.getX() and myObject.getWidth and then the set methods accordingly. What I am looking for is eligance. The attribute syntax has it… and the Java style getters and setters don’t.
But then again, maybe I am just easily annoyed.