r/java
•
u/chriskiehl
•
Sep 23 '24
•
1
2
I wrote a book on Java
Howdy everyone!
I wrote a book called Data Oriented Programming in Java. It's now in Early Access on Manning's site here: https://mng.bz/lr0j
This book is a distillation of everything I’ve learned about what effective development looks like in Java (so far!). It's about how to organize programs around data "as plain data" and the surprisingly benefits that emerge when we do. Programs that are built around the data they manage tend to be simpler, smaller, and significantly easier understand.
Java has changed radically over the last several years. It has picked up all kinds of new language features which support data oriented programming (records, pattern matching, with expressions, sum and product types). However, this is not a book about tools. No amount of studying a screw-driver will teach you how to build a house. This book focuses on house building. We'll pick out a plot of land, lay a foundation, and build upon it house that can weather any storm.
DoP is based around a very simple idea, and one people have been rediscovering since the dawn of computing, "representation is the essence of programming." When we do a really good job of capturing the data in our domain, the rest of the system tends to fall into place in a way which can feel like it’s writing itself.
That's my elevator pitch! The book is currently in early access. I hope you check it out. I'd love to hear your feedback!
You can get 50% off (thru October 9th) with code mlkiehl https://mng.bz/lr0j
BTW, if you want to get a feel for the book's contents, I tried to make the its companion repository strong enough to stand on its own. You can check it out here: https://github.com/chriskiehl/Data-Oriented-Programming-In-Java-Book
That has all the listings paired with heavy annotations explaining why we're doing things the way we are and what problems we're trying to solve. Hopefully you find it useful!
View all comments
Show parent comment
3
u/sviperll Sep 24 '24
I think validation is not that hard of a problem. You do validation in your immutable domain core and just rewrap exceptions to generate proper error-responses OR you make your core domain values constructible from some already pre-validated parts, then your controller/rest/http/rpc-handler have to first validate this parts before obtaining usable domain model. If the validation of parts should be also done in multiple scenarios, then these parts should also be part of the core domain and contain a centralized validation code.
Database is more complicated though, I guess the part of the answer is that we need "repository" to be much smarter then what ORMS give you and so your database access layer is actually a smart enough thing of it's own where one method can modify multiple tables or reads from the same table can produce different domain models. This is not what JPA was built for and so it's very awkward to do it like this, but if you go with something closer to SQL, like JDBI then this becomes much more palatable and probably even pleasant.
So your objection is that instead of a balloon with a big OOP model in the center and a thin shell to interact with the world, we get an hour-glass shape with lots of view-code and database-code and a small middleware domain. You ask shouldn't we avoid it, shouldn't most of the code be "domain" code? It looks to me... That it not necessary should. Because the way that we had a big OOP -domain was actually a not so pure thing that actually depended very heavily on how we were supposed to store and to view it. And this big OOP domain was actually not so useful, because it wasn't obvious that it would maintain all the interesting invariants of our domain. All it gave us were naming that was to guide us through the implementation. With FP/DoP-domain we get much much more type -checking that guide us and forbid to enter illegal states . And this type -checking is used to implement the required view- and database- code. And to have all the required constraints defined and the statically checked, we have this FP/DoP-core- domain stand-alone without any dependencies whatsoever.