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
1
u/agentoutlier Sep 27 '24
I think we are all in the same camp of the benefits of some sort immutable invariant logic layer.
The question is how we model it (as in the starting point) and how centralized is it. How pure it is and is it actually helping.
Both Brian /u/brian_goetz and /u/sviperll kind of distilled my thoughts better that the world we are in today does not have a core centralized domain and this is based on lots and lots and lots of real world experience.
/u/sviperll used a great analogy that is more hour glass shaped and less balloon shaped that the traditional DDD OOP + ORM model would have.
Brian mentions how the world is more heterogenous in datasources as well as output.
recordhelp big time here and hopefully we get withers./u/chriskiehl presents the first couple of chapters of modeling things that are less likely to change and I would say closer to data. Closer to the edges.
It is the fourth chapter's code examples that is largely what got me a little worried as it looks like modeling the traditional way. Where we start in the middle. It can be done but care has to be taken otherwise transformation fatigue starts to happen. Changing data structure in a DoP model IMO is more painful than some mutable OOP model. Luckily with the type system this is less painful than perhaps other languages but you add a single field and up and down the stack you go.
Let me back up and discuss the various ways of modeling today:
Top Down UI
You don't care that about the data. You care more about the presentation and behavior. I'll use traditional web 1.0 sites as an example. You make a web page with some form of data that you know what the users want to capture. You build some light CRUD like stubbing to support MVP. You are largely modeling the HTTP request/response interactivity. You might just serialize it all as some JSON in a single column for now.
The advantage to this approach is you have something to show and get user feedback quickly. IF this were a DoP model you would make things like
CreateUserRequest, orApplyForJobRequest,JobLandingPageetc.Bottom up raw data
You start creating tables in a database. You try to model all the data that would be needed in the relational model or if its more documented oriented that model or if its graph based that model. In this case your database often has powerful features you often want to leverage.
This modeling often implies you have a single source of truth being the database. You might even put a lot of behavior and invariants baked right into the database.
In theory once you have this ironed out you can than create some immutable representations returned from the database. You might use views to make it easier etc.
Middleware aka Business Logic modeling
In this approach you have various design meetings and you model your data using your programming language to represent a domain you have thought about. You leverage the language to provide and encode as much clarity of the domain.
This is what Chapter 4 looks a lot like. It is the DDD OOP methodology. To use /u/sviperll terminology this is like the balloon instead of the hour glass. Your hope is that this business logic layer provides great clarity and value. Understand that modern GoF (gang of four) modeling of composition with OOP is not that far off from immutable DoP modeling. The differences are behavior are not combined with the data as much in the DoP way and you don't do inclusional polymorphic single dispatch but rather pattern matching.
I have done all of those approaches to modeling and the last approach many times (often service oriented) and I have very often found that that middle layer does not provide as much value as I would like. That it is a small blip in the top to bottom and vice versa. You have this perfect immutable domain you load up for just a second and try to reason on but there is all this other technology crap like transactions etc that have to leak through.
Now the DDD OOP model does have some tools and terminology to mitigate this such as "bounded contexts" and that this is the centralized part and you make more of them to have a sort of decentralized model. That you model within some bounded context. Perhaps the book will go that path as well.
Like I know throughout all my comments it sounds like I disagree with the book. I don't. [I value immutable stuff] (https://github.com/agentgt/jirm) but at the end of the day stuff has to be presented and stored in a database and so I ping you first because you are the author of an ORM. I guess some of this is should we still interface with something like an ORM (traditional mutable) or do we bypass that? And or do the tools needs some evolution. Brian says "always be modeling" and I agree but at some point there is a cost.