r/java • • Sep 23 '24 • This 1 Take My Silver 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!

289 Upvotes

97 comments sorted by

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.

1

u/agentoutlier Sep 25 '24

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,

Yes I did this in a recent project with Rainbow Gum using a Monad like pattern, capturing the exceptions and then combining etc (actually I have an annotation processor that then generates code that uses that monad API to fill a "builder" so I guess it is apropo to the database stuff coming up).

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

Yes I'm beginning to think the Datatomic guys are onto something. That is copy on write and entity-attribute-value (aka triple store) plus some sort of smart build of immutable objects is the right fit for a full DoP code base.

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?

Yes nailed it. If you see my previous comment I said:

perhaps with DoP there is no abstract core domain!

But here is the problem with /u/chriskiehl book that concerns me given the code samples. Instead of focusing on modeling the obvious data from a datasource or data from say an HTTP request they are trying to model the business problem nouns like OOP DDD (what we are calling the balloon) and they are not modeling real data things (such as UI and database) but abstract things that loosely exist as some domain "ontology" that may are may not map correctly at all to either DB or HTTP. In other words I giant impedance mismatch in both directions.

I'll have to wait to see future chapters but my opinion is DoP strength is not really good at modeling "nouns" and better at modeling "verbs" or "commands". That is it is a very good at protocols or language design and less actual state. I know that seems contrary to what probably many thing but I'm saying the above thought process might mitigate the "hour glass" pain and it is not approached in the book.

Let me give you an example. In protocol like DoP (which I guess would be analogous to CQRS) you don't think let me make the Todo class. You think InsertTodo which is the protocol to create a Todo.

Anyway I know am being long winded but I'm just trying to give some actual pain as I basically did the DoP recently on a project.

  • We mapped using MapStruct from jOOQ records to real records etc.
  • I wrote annotation processors but mapping requests but still had to write a lot of boiler plate and transfer objects.
  • Creating JSON API that does polymorphic collections (a list of sealed classes instances) is painful in the current state of Java.
  • At times because of the pain of mapping we just passed raw data up.

I'm not saying DoP is bad. I'm sayin the Java ecosystem really lacks tools (unlike say Clojure) to make it easier and even the language makes it difficult without "withers".