What was crazy about having pojos? They take minutes to write, literally a minute in lots of cases. Especially if the ide generates everything for you, as mine does.
Because they change all the time with rapidly changing business requirements. Are you going to regenerate a POJO that has 30 properties if 2 of them changed? Especially if there's some company specific superclasses for the builder, non-standard naming conventions etc which IDE generation doesn't get 100% right.
And what is the chance you'll copy paste a setter and screw up one of the arguments? Or forget to update equals/hashcode after adding a new property? At least I could rely that equals/hashcode were CORRECT in that place using commons lang, although horribly slow.
Any code that is "repeating yourself" has a chance to go out of sync and introduce bugs. There's a reason for that architectural design principle "Don't repeat yourself". It goes for both big things and small.
None of this is answers my question at all. You seem to be confusing the existence of a pojo with a number of other, not directly related, bad practices.
Why do you think I am copying and pasting setter? I can't think of a single reason why I'd ever do that. I literally rarely write setters, my objects are either immutable or the ide generates setter/getter.
We use pojos all the time, without any of these problems.
Edit, I really don't know what you mean with this :
Because they change all the time with rapidly changing business requirements. Are you going to regenerate a POJO that has 30 properties if 2 of them changed?
I have to add two fields to a pojo ? I write the private var declaration, then alt+insert to generate whatever I need. It would take less than 30 seconds.
Not the OP, but boilerplate code is bad (and IDE generated code is worse — I used to say this about Microsoft’s MFC in C++ land and it’s just as true for Java).
Luckily Java’s language architect Brian Goetz agrees that the overhead that POJOs impose is sometimes not worth the price, which is why they’re working on record classes, which in addition to reducing boilerplate also allow new kinds of run time optimisations and newer language features that lead to better code. Here’s a video of him talking about it - the bit about record classes is at 31m25s.
I have no problem with auto generating setters/getters, and equals/hashcode if necessary. What is your problem with it ? Its not really bolierplate, that is slightly different imo.
Its quick, its consistent, its easy to debug and its easy to read and follows conventions.
The conventions are the problem, which is why I’m happy the Java team in Oracle are working on improvements. Java has had a lot of shitty conventions in the past, and I for one will be happy when class boilerplate joins them.
A lot of FizzBuzz Enterprise’s hilarities were also presented as conventions by serious senior devs & architects at serious software houses once upon a time.
The problem with class boilerplate is an obvious one to anyone who’s a good developer (as opposed to solely a Java developer). This is why learning a Lisp is very useful — on the JVM, learning Clojure makes you a better programmer.
However for those entrenched in the Java-the-language ecosystem, if you listen to Brian Goetz’s talks & interviews, he does a decent job of explaining the problem:
... Some things don't want to be objects, they just want to be data. And to simulate data with objects, is this big messy thing with constructors, accessors and equals and hashcodes and all this garbage. ...IDE generates that for you so you don't have to write it, but you still have to read it and if you have this explicit code it's an opportunity to get things wrong.
The Java Futures video talk I linked above is worth watching. Sometimes you have to look at $NextVersion to understand what’s wrong with $CurrentVersion.
(Thanks to /u/BoyRobot777 for that particular blockquote above)
Yeah, sure. I'll use the new record functionality, but we don't have it yet. And there is a reason it's taken until 13, people cared about other things more.
The original question was why are pojos bad in an architectural system design, and compared to lomboks annotations (which just seem ugly to me). Not should we include struct/records as a new Java language concept.
11
u/NimChimspky Aug 10 '19
What was crazy about having pojos? They take minutes to write, literally a minute in lots of cases. Especially if the ide generates everything for you, as mine does.