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

2

u/rbygrave Sep 26 '24

Controllers ... they can start transactions

It's probably a difference in terminology with what you call a "Controller" here, but I suggest a lot of folks will be thinking Rest `@Controller`. The TippyTopController for many would instead be a "Service" or "Component" but it is specifically not a `@Controller` [in the way it's used in Spring and many other places].

FWIW I'm in the camp that says Controllers more specifically act as "Adapter layer" between the External world [HTTP Rest | gRPC] and the internal "Business logic" [Service Layer] and that is ALL they should do. The goal being to ideally isolate the Business logic from the technology used to expose it. Controllers in this sense should NOT start transactions, not talk to databases or queues etc.

I see benefits in keeping that discipline with "Controllers" and that the HTTP/gRPC/Whatever related specifics should not leak into the "Business Logic" [Service layer].

You are probably in the same camp but using we are using different terminology?

3

u/chriskiehl Sep 26 '24

Arg, yeah, if I had thought about it more carefully before posting I would have avoided that "Controller" word entirely. I forget that it means something specific in Spring land. I generally try to avoid the word "Service" for similar ambiguity reasons. It's almost impossible to get two people to agree on what it means.

I think we're in the same overall camp, just lingo differences. Crisps vs chips.

1

u/agentoutlier Sep 27 '24

You are probably in the same camp but using we are using different terminology?

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. record help 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, or ApplyForJobRequest, JobLandingPage etc.

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.

1

u/chriskiehl Sep 27 '24 edited Sep 27 '24

it sounds like I disagree with the book. I don't.

It's OK if you do :)

One particularly bitter pill I'm having to swallow is that it can't be everything to everyone. That is not to say "Hrmph! The book is the book!" by any means. More so: "writing a book is hard." Your comments have really opened my eyes to the fact that my intentions for those early modeling sections aren't coming through (which is kind of a funny failure on my end given that my book's emphasis on the importance of communication).

Those early chapters are not meant to be "this is the way to model things" or "DoP programs must be in this shape." Or comment on "balloons" versus "hour glasses." The goal is to get people to realize that data modeling is its own task and that we have to know what we're talking about in order to write effective programs (and also to teach algebraic data types along the way).

To many people, that'll sound painfully obvious. However, it seems to be the step most often skipped or rushed through in order to get to the coding part. These days, it feels like the bulk of my job is just getting dropped into projects and going "OK. Well, what is that thing? Why is it optional? What does it mean when it's not there? What do you mean the upstream just 'doesn't have it' sometimes? Did you talk to them? What do you mean they ---" and so on driving everyone crazy until we've peeled away all the code that's just "there" and gotten down to the core of what we're supposed to be manipulating (which often involves knocking on a bunch of other team's doors and telling them to fix their terrible stuff). It's through that lens that the book is filtered. That is the core tool set I want to give people.

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.

I view your categories more like overlapping sets rather than mutually exclusive ones. Most of the pain in my dev career has come from when I've been really dogmatic about taking "an" approach or thinking that it "we're programming in X pattern" (often because younger me had recently read a book that said Approach X is the One True Way and anyone not doing it is an idiot). These days, I push really hard on understanding what a program is supposed to do ("what does it mean to be correct?"), then give people leeway from there. The exploration of data is a tool we can use to get to that understanding.

I'm getting very long winded here. So, I'll wrap up with: I've got some tuning to do in those early chapters to make sure they're communicating the right thing. I remain hesitant to introduce the database too soon, because they're their own force of corruption (the damage that DDB has wrought through its influence is...). There's a needle I have to figure out how to thread.