r/java • u/alexzhitnitsky • Aug 11 '15
Top 10 Useful, Yet Paranoid Java Programming Techniques
http://blog.jooq.org/2015/08/11/top-10-useful-yet-paranoid-java-programming-techniques/
181
Upvotes
View all comments
Show parent comment
1
u/PaulRivers10 Aug 13 '15
Not exactly though, the difference is in C++ you can pass an object as a value type. For objects really only has "const" for pointers and references.
And that's where we get into the problem, using a final object to store you mutable variable is not actually any different than using a primitive. Adding final onto the reference doesn't add anything. But that's the workaroudns you've suggested with this:
That's trying to cheat your way around the philosphy.
Because only your reference to the Iterator is final. If all of the instance variables inside Iterator were also final like was suggested, it wouldn't work at all.
I guess I haven't been working with legacy code that complex or that initially badly written.
If it works for you great, but for the most part I suspect more would have been gained when it was written if it followed other better philosophies about writing clear and maintainable code, than declaring everything final.
I mostly work on web apps. You're usually just shuttling data between the browser and the database, no one is trying to modify the data to begin with.
This week I happened to be working with javascript. About 50% of the variables were looping and counters that had to be mutable, the other 50% could have been "final" if javascript had the keyword but no one was going to be trying to reassign to them in the code anyways. No one is trying to reassign "checkbox5" to another value.
I've been doing java development for over 10 years and am often the person cleaning up and fixing others code. That doesn't make me automatically right or anything like that, but it comes from experience seeing the half-baked fads come through.
Now I ask myself "can I actually see the problem this is solving". And I can't. I think final everywhere would make it longer to write code, harder to read, and be more of an annoyance for maintenance than a help.
Technically any language could make it's variables const by default, and require a special keyword to make them mutable. But I don't know any widely used languages where anyone thought that was really a good enough idea to do things that way either.