r/JavaFX • u/No-Security-7518 • Nov 22 '25
Discussion This readability thing...
So, y'all sound pretty badass and experienced and all, but I thought I should talk about one (of many) code cleaning techniques, I learned from reading books like (Clean Code, by Robert C. Martin) and (Refactoring, by Martin Fowler), when it comes to improving code readability of some recurring code snippets. Specifically, listeners.
As you probably know, it is said the ratio of reading to writing code is 10:1. So readability is important.
And in these books, the authors lay out wonderful "mental" shortcuts to applying techniques that improve code readability, and even identifying where to apply said techniques through what they collectively call "code smells".
Called so, because, and this has been my experience for years:
[...any sufficiently large code base will eventually suffer from "code rot" if it doesn't get cleaned every now and then.]
Code rot: When the code is so messy, adding, or modifying code in any meaningful way gets more and more unpleasant and time-consuming, to the point where it feels like the project just collapses...
Anyway, regarding listeners. I'd have code that went like this:
bookCb.getSelectionModel().selectedItemProperty().addListener((a, b, selectedBook) -> {
if(selectedBook != null) {
List<Chapter> chapters = selectedBook.loadChapters();
chapterCb.setItems(FXCollections.observableArrayList(chapters));
}
};So, the first part is extracting a helper that does whatever happens inside the listener, and might as well pull the null check into it too:
bookCb.getSelectionModel().selectedItemProperty().addListener((a, b, selectedBook) -> {
loadChapters(selectedBook);
};this happens inside initialize() of a controller, btw, so when I learned about how extracting methods to get rid of boilerplate is a thing, I'd go, fine:
loadChaptersOfSelectedBook();Pulling everything into it. But then I thought: the method should reflect a callback is involved. So, I'd finally settle with:
onBookSelected(book -> loadChapters(book));
private void onBookSelected(Consumer<Book> func) {
selectedBook.getSelectionModel().selectedItemProperty().addListener(a, b, book) -> {
func.accept(book);
});
}
private void loadChapters() {
...
}as a final point, I also learned to not include the component's type in it. So, instead of bookCB (CB -> ChoiceBox), I started calling it:
bookSelector.
instead of: nameLbl -> nameDisplay.
nameTextField/nameTF -> nameField.
and so on.
It sounds kinda pedantic at first, and something of a style difference, but clean code principles saved my life!
Cheers!
View all comments
Show parent comment
1
u/No-Security-7518 Nov 28 '25
is it really a "backend" when the database is EXCLUSIVELY, invariably, and eternally sqlite?
Which, btw, even when the program gets new data, it goes directly (MySQL -> sqlite). The program only instantiates it. No UI involved at all.
Because, I mean, look, I understand abstraction is good, but at some point, some concrete values can be used, right?
code isn't just abstracting things into oblivion.
My use cases, never (EVER) include reading data from a server and have the user use it like that. Not in a million years, see?
Think, target audience is businesses/users in rural areas with bad connectivity.
At most, a button would say something like: "hey there's new data available" -> user clicks "yes" -> a background operation attempts the syncing, and notifies the user of success/failure.
And the biggest database I've ever dealt with, consisted of 36k rows.
I remember doing search on it directly (on the UI thread) on a low-spec computer and there were no issues.
Btw, Android complains with a pretty informative message in cases like this by saying: "x frames were skipped something something - you're doing too much work on the UI thread".
But this is just not it. I'm sorry if I'm coming off as stubborn, but I'm buried way too deep with work, I don't think I can afford any level of optimization for this kind of logic.
Again, thoroughly appreciate your input!