The project I've been assigned at work is a small one, but I've been keeping 100% coverage on the things that I care about - the things that I think are important. However, it sucks to have the metrics that will be looked at by management to be less than 100%. "It's okay, because that code isn't important!" Stupid getters and setters didn't have 100% coverage...
In my search for the One True Answer, I came across an interesting post that's the closest to my feelings on it. If I've gotten uncovered getters / setters, then I might not actually need that code. That was a big part of my solution today - deleting setters.
My whole issue with this is that now I'm taking time to think about code that I generate and for all intents and purposes has no effect on my project. Does it matter that a model object has an unused setter to a field? I don't think so. It can show that you don't have a test case covered, but if your only way to tell if you are missing an important use case is an uncovered getter / setter, you might have other problems.
What I wish they built into java was something along the lines of Ruby's attr_accessor keyword or the similar solution in just about every other language. There's even a feature request for it in java - but since it's over a decade old I'm not going to hold my breath.
The fact that there are so many discussions, work arounds, IDE tools, scripts, etc should indicate that it's a problem that should be solved on the language level. The fact that I was thinking about it today at work was a waste of time and money. Uncovered accessors effecting the perceived quality of an app I work on means 1) the metrics need improvement 2) I'm probably not the only one wasting time on this issue 3) we're discussing field accessors when we could be solving an actual business need.
It's stuff like this that makes me wonder how long it will be before enough people move to other solutions like groovy, ruby, phython, etc. before the people in control of java sit up and take notice and cut the cruft out. At that point it'll probably be too late. It's really hard to un-sink a ship. :-/
Yeah, .Net has had support for Properties for quite a while. I think since it was first released. Really great feature. Although I would agree with the comments about that "feature request" that it really is just syntactic sugar. It doesn't allow you to do anything that you couldn't do without properties.
ReplyDeleteI would say that it does add 1 thing. It's a clue to people reading your code that you chose to use a property, and that means that the method shouldn't have any side effects, and it also gives a clue to the compiler / runtime, so that when you are debugging your code, it assumes it's OK to run that method (properties compile down to methods in the end), so that hovering over the property with your mouse will show you the value.
In Java, the only assumption you can make is that if a method starts with "get" then it simply retrieves some member variable, without any side effects, although I don't think I would want to depend on that, as many methods coulds start with get for no apparent reason.
One last thing, in C#, you can define an Auto Property, by using the syntax:
public string FirstName { get; set; }
that's it, and they're adding a similar feature in VB.Net in the next release.
I think that this is the one major difference between the .Net and Java platforms. .Net evolves their languages, and take on many new languages, like Python, while Java tries to keep the language the same over time. I'm not sure which is better.
I'm not sure that I'd save that Java isn't evolving. Just take a look at the wikipedia page: http://en.wikipedia.org/wiki/Java_version_history
ReplyDeleteMore and more java and .NET look the same to me: both use a vm, have memory collection, and support multiple different languages
http://en.wikipedia.org/wiki/Da_Vinci_Machine
And there isn't anything wrong with adding sugar. ;-)
When I say that Java has been stagnant, I was referring more to Java the language, rather than Java the platform. Based on the link you posted, since Java 1.2, they've added assert, Generics, Enumerations, Varargs, For each loops, and it seems like that's about it. None of those are very exciting,
ReplyDeleteMeanwhile C# has added things like lambda expressions, anonymous types, extension methods, dynamic variables, and optional parameters, just to name a few. C# seems to be adding a lot of things to make it more like the popular dynamic languages such as Ruby.
That isn't to say that one is better than the other. I personally haven't really found much of a use for most of those new programming constructs.
Oh, and I agree. Nothing wrong with sugar. Although I can see where they are coming from.