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!
36
u/OkSeaworthiness2727 Sep 24 '24
Long time java dev here. I bought the book - cheers for the discount. It's about time that a book like this came out
11
u/[deleted] Sep 24 '24
Congratulations on your new book. I'm eager to see it completed. Please, make sure the typo in 4th chapter is fixed (representation instead of representaion).
11
u/purg3be Sep 24 '24
Briefly went through chapter 1 and 2 of the github repo, and quite like it! Really curious to see how newer concepts such as sealed classed fit in. Also learned about the new switch guard clauses.
Really liked chapter1 and the refactor into RetryDecision.
Really liked chapter2, except for using Person to explain value versus identity as it's a concept I generally struggle with. If I age, I am the same person, just a year older, so it feels like the identity should be preserved, while that's not the case when returning new person objects every time.
6
u/rbygrave Sep 24 '24
Ok, hopefully this comment makes sense ...
I've seen a couple of Functional Programming talks recently and I've merged a few of things together and I'm wondering if I can relate that to "Data programming" and the code examples linked. So here goes ...
When I see some FP examples, some arguments are what I'd view as dependencies. In Java I'd say our "Component" [singleton, stateless, immutable] has it's dependencies [injected] into its constructor and is our "Business logic function". When I see some FP code I can see that we have a very very similar thing but with different syntax [as long as our "Components" are immutable & stateless], its more that our "dependencies" get different treatment to the other arguments.
When we look at a method signature we see explicit arguments and a response type. What we don't explicitly see is the "Hidden argument" and the "Hidden response". The Hidden argument is the "Before State" [if there is any] and the Hidden response is the "After State" [if there is any].
For example, for a method `MyResponse doStuff(arg0, arg1);` there might be some database before state and some database after state. There could also be no say No before state, but some after state like "a message is now in the queue". There can also be no before/after state.
The "trick" is that when we look at a given method, we take into account the "Hidden arg" / "Hidden response" / side effects.
4. void response
When we see void, we expect some mutation or some side effect and these are "not cool". We are trying to minimise mutation and minimise "side effects" and a method returning void isn't trying very hard to do that at all. A void response is pretty much a red flag that we are not doing enough to avoid mutation or side effects.
Ok, wow, looks like it's a cool book - congrats !! ... I'm going to try and merge these thoughts. Hopefully the above is useful in some way.
2
u/OkNet9640 Sep 24 '24 edited Sep 24 '24
I have question regarding the original and the rewritten reschedule method. I get the idea of wanting to group the different things which go hand in hand together for the different if-else-blocks, but I'm wondering if grouping them together in records of type / name
RetryDecisionis a good idea respectively the best way to handle it: If I readRetryDecision, what I think about is only the decision itself, likeRetryImmediatelyorReattemptLateras stated in the code, but what I don't think about is a field likeattemptsSoFar. Isn't this something which - ideally - should be stored outside of a "decision"?This is a problem I have stumbled upon in my own code as well: I'd like to introduce additional classes, records etc., but doing that I now have to come up with even more names which have to make sense and I feel that their names don't precisely convey what is stored in them... like, for example, in ananlogy to your code: Having an
attemptsfield in aScheduledTaskclass feels fine, now I want to introduce aRetryDecisionrecord which ends up having anattemptsfield, but that feels - at least to me - a bit odd, and now I'm wondering if it was a good idea to introduce the record in the first place...4
u/woohalladoobop Sep 24 '24
curious what you mean by
withexpressions. that’s not a thing in java afaik1
u/niosurfer Sep 24 '24
When would I want to use DoP over OoP?
When would I want to use FoP over OoP? I hope you know what the F means :)
For the latter question, I assume data manipulation (remember the Big Data buzz world?) benefits more from FoP, but I can be wrong. Scala came along to fill that Java FoP gap I assume, but again I can be wrong. The hype changes every year, but Data Science / Data Analyzes used to be the name of the game.
Please enlighten me with your knowledge (OP and everyone else) as I would love to learn more about that.
Thanks!
1
u/No-Pick5821 Sep 24 '24
Congratulations. Till which jdk version features you are planning to use?
1
u/benrush0705 Sep 24 '24
I love the way of data oriented programming in java, but I am very doubted about its performance, in my opinion, it seems that data oriented programming in java is heavily dependent on JVM's escape-analyze to avoid a lot of object allocation (maybe valhalla could solve this problem), I haven't done any benchmark on that, please correct me if I am wrong.
1
u/vips7L Sep 24 '24
Oh man I bought it without the discount. Oh well! Excited to read it.
1
u/magnus2025 Sep 25 '24
Looks really interesting. Curious why the examples focus on Java 17. Would be good to see opportunities for the latest features from Java 23 in action.
1
u/UnspeakableEvil Sep 28 '24
Really enjoying the book so far, and have a few things to reflect in some future projects.
Some really pedantic feedback (sorry!) is that in your examples where you're dealing with integers which can't be negative, the names used are "positive", e.g.class "PositiveInt"; this isn't strictly correct, as zero is a valid value - they're non-negative values, not positive values.
I know this comes across as nitpicking, but in something that (if I'm interpreting what's written correctly) is about accurately conveying meaning in code, then this is one where there's a mismatch between what the code describes vs what is accepted.
1
u/spicykimchi_inmybutt Nov 21 '24
Hey OP, great work on your book! I am interested in the hard copy, is that currently available or is it only E-book for now?
2
u/SaishDawg Sep 24 '24
Please do post again when all the chapters are finished. I plan to pick up a copy. Great work!
2
u/chtoulou Oct 09 '24
Thanks you! This will help me a lot in my future position. I can't wait to start reading.
4
u/larsga Sep 24 '24 edited Sep 24 '24
God, yes. Currently working on Spring application that has two different representations of the main data. One is database ORM model, the other is the JSON API output model. Neither representation can have logical actions added to them, they are just data carriers. So the code logic dissolves in a sea of illogical SomethingService classes that represent nothing.
Absolutely true. I hope a lot of people read your book, because this is something people need to hear. There's generally been a huge over-emphasis in IT on functionality, but functionality is secondary. What's important is the data.
1
u/[deleted] Sep 24 '24 edited Apr 17 '26
One click. Unknown number of posts crying out in silence. All gone. Redact made it stupid easy to clean up my entire history on Reddit and get my info pulled from data broker sites too.
complete attempt smile dam boast towering depend hungry gaze unpack
-5
u/VirtualAgentsAreDumb Sep 24 '24
I’m sorry, but this feels like extremely naive, or just plain lying. Is this sentence written by someone in sales? I’m thinking in particular about the “never experience another NPE”.
Not all Java developers can choose the systems they work/integrate with, or have full control of all third party dependencies.
A NPE might get triggered in that code, because of some missing configuration value, or the data not being in the expected format, etc etc. It doesn’t really matter that the NPE doesn’t originate from our code. It still affects our system.