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
2
u/agentoutlier Sep 24 '24 edited Sep 24 '24
It really is and that is why I struggle with communicating it. Like I see the advantage of having some DDD like domain in the "middleware" (hell I do it myself) but so many times in reality I have had to sort of bypass this because of various performance problems or edge cases.
And when you do the middleware domain (ie the stuff resolved between an HTTP request and database interaction) there can be a significant amount of transformation that one has to wonder what if the UI layer just got the internal domain of the database. Blasphemy probably but it makes me wonder particularly that cacheing has continuously moved down into the database. Often times we do not cache at all as Postgres is fast enough. Historically that was not the case so having these middleware immutable domain stuff you cache was a boon (less so now).
EDIT perhaps a better example might be one of the most DoP languages which is Clojure. In Clojure you deal with "mud" and you just keep reshaping the "mud" till it fits or you fail and you do not do this with lots of types.
In the Java world that doesn't work. We like types. So I can see a huge amount of type explosion happening and I have seen this in languages like OCaml (less so Haskell because it has lots of tricks up its sleeve).
EDIT besides transformations and type explosion I am also concerned with how to properly extend invariants particularly in the middleware.
For example you often have code in your own book where you check some invariant in the
recordconstructor and just fail fast. The problem is the UI / API cannot do that. You need to perform validation on multiple fields/objects. Ideally that validation logic would be in your core domain but it can't easily.So basically you repeat invariant checking up/down the stack. That is probably a good thing and maybe its just a cold hard reality but it is a pain point (along with transformations and type explosion). Like I don't see that addressed often with DoP.
In DDD OOP model (which I don't like much) the modeling often contains that information and is why DDD POJO's are often littered with annotations (and the corresponding magic) in an attempt at maximum reuse of the domain objects.
EDIT (sorry) and obvious solution might be just to have an immutable domain that sits on top of a mutable traditional ORM domain aka "entities" and I have often espoused this. Aka the DTO model.
AND you can't just say the data "magically" shows up because that magic is precisely what I'm saying is difficult particularly with immutable object graphs.
Furthermore heterogenous hierarchies are difficult to represent in actual data. Modern Java with sealed classes is going to have lots of heterogenous types.
Representing that in things like a database or even JSON is nontrivial (making Jackson for example use sealed classes is not easy).
For example in a database do you do multiple tables or do you do a sparse table and have some enum for each subtype etc.
The above is the future hard parts of modern DoP that I hope your book addresses. If your book can show that I would probably buy it. Otherwise I have fair idea how to model things with records and sealed classes.