5 ms·
The #1 thing you need to make Java usable is to abandon the JavaBean conventions. When every field requires 8 lines of boilerplate it's no wonder the code looks
by lmm 13y ago
The #1 thing you need to make Java usable is to abandon the JavaBean conventions. When every field requires 8 lines of boilerplate it's no wonder the code looks ugly (YAGNI, and if you do need it it's two keystrokes in your IDE to "encapsulate field"). public final fields are fine, and can get your data classes something close to readable.
I'd stick with maven for the build rather than Gradle; it's completely declarative and all the tools understand it. Learning a new language just to configure your build tool seems excessive.
- exabrial 13y agoCouldn't agree more. Use public, protected, private fields as they were meant to be used. I only dip into JavaBean anymore if I need a readable but non-writable field.
- karlmdavis 13y agoFor me that's almost all fields: I try to make just about everything immutable by default. Makes reasoning about concurrency much simpler.
- lmm 13y agoFor immutable you can just use public final fields. I assume GP must be talking about fields that are mutated but only by the object itself, where outside code needs to be able to read but not write.
- stevoski 13y agoNot quite. eg: public final Date date; date.setTime(1); That's not immutable.
- lmm 13y agoThe bean convention doesn't help you with that one though; if you have private Date date; public Date getDate() {return date;} then people can still do getDate().setTime(1);
- cdmckay 13y agoYeah, but with a getter you can make getDate return a copy of the Date object to enforce immutability.
- cakeface 13y agoYeah, but getDate().setTime(1); works the same way.
- djur 13y agoNot if getDate() returns a copy of the underlying member.
- mateuszf 13y agoYep, the thing is that Date is immutable. I think that this public final immutable rule is ok, but one has to ensure the fields themself are really not modifiable.
- nl 13y agoThe fact that java.util.Date is terrible is so many ways should go without saying.
- sztanko 13y agoProject Lombok is your friend then: http://projectlombok.org/features/Data.html http://projectlombok.org/features/Data.html
- lmm 13y agoUntil you have to debug it. At which point you realise you're now writing what's effectively a different language with its own tooling and compilation (and its own bugs), and if you were going to do that then you might as well go all the way to Scala where you get much more in return.
- arel 13y agoLombok is an anti-pattern for me. It creates a unholy mess of annotations for what should be language features.
- emsy 13y agoFor starters, you can see Groovy as "Java without semicolons". I went from Maven to Gradle and never looked back. It's superior in most ways. The tooling could be better though.
- lmm 13y ago> For starters, you can see Groovy as "Java without semicolons" Not if you have to read other people's code, or understand examples you find on the internet. > I went from Maven to Gradle and never looked back. It's superior in most ways. What's it better at? I want my build tool to be simple; maven compiles my source and does my releases, and the main thing I have to configure is just a list of dependencies (in an admittedly verbose format). I'm actually a scala programmer, but I use maven rather than SBT because it seems to me that having lots of logic in the build system could only lead to bad things. So what are the things you see it helping with?
- dminor 13y agoMaven is simple as long as your build is simple. As soon as you need to do something custom, it's a pain. Gradle is less painful.
- emsy 13y agoSee my reply to vorg regarding Groovy. As a developer the argument "I don't want to learn something new" is invalid. Gradle offers the declarative nature of Maven without pushing it down your throat. You don't have to write a plugin for something that can be expressed in 3 lines of Groovy (but you can, if you want to!). Instead of adapting your build to Gradle, Gradle adapts to your needs. That's often a point of criticism from Maven users, because every Gradle build looks different. But that's the point: Everyones needs are different. Of course that only applies if your build is beyond the standard compile/test/release configuration. A simple configuration looks pretty much like a Maven POM (minus the tag soup).
- vorg 13y ago> you can see Groovy as "Java without semicolons" Only a very small subset of Groovy is used by the typical Gradle build script, the very subset of Groovy that's least like Java. What part of this build script from the linked article bears any resemblance to Java?... apply plugin: 'java' apply plugin: 'application' sourceCompatibility = '1.8' mainClassName = 'jmodern.Main' repositories { mavenCentral() } dependencies { compile 'com.google.guava:guava:17.0' testCompile 'junit:junit:4.11' // A dependency for a test framework. } run { systemProperty 'jmodern.name', 'Jack' } javadoc.options { docletpath = configurations.markdownDoclet.files.asType(List) // gradle should relly make this simpler doclet = "ch.raffael.doclets.pegdown.PegdownDoclet" addStringOption("parse-timeout", "10") } run { jvmArgs "-javaagent:${configurations.quasar.iterator().next()}" // gradle should make this simpler, too }
- levosmetalo 13y agoI used Project Lombok back in the days to get read of Javabeans boilerplate. Just declare your private fields and put @Data annotation on class, and it would generate at compile time all your getters, setters, equals, toString, constructor and hashCode methods. Really helps to keep the code lean and small. Don't know does it work with Java 8 though or it didn't yet catch up.
- mschulze 13y agoI used Lombok in a Java 8 project without problems.
- umut 13y agoCouldn't agree more.. But this comes with the most cliche discussion with a smartass looking at your code. Dude, I know what getters and setters are, I just don't agree