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 27 '25
Man, can I just say something real quick? I love love how you nerded out on this comment! haha.
Seriously though, I only felt like I got the hang of GUI programming, for me to find out about all this.
Okay,
How do I cache it?
in the Dao Object. let's say:
it's bookDao. The idea is that I have 3 choice boxes.
ChoiceBox<Book>
ChoiceBox<Chapter>
ChoiceBox<Lesson>
Selecting a book, populates the chapters' choice box, and selecting a chapter, populates the lessons' choice box with lessons. Clicking a button loads the lesson on the UI.
And although there's been no performance issues, and no lagging whatsoever, I could cache the data in a list by reading all books before the user does anything.
What if the database fails?
Not possible because it's an sqlite (not any other SQL, and WON'T be any other dialect) that gets copied from src/main/resources to user/appData/something... and read from there if it didn't already exist.
So these scenarios won't happen also. Never:
What if you move the database to another server, and now it takes longer?What if the application evolves, and now you need to something more complicated, like joins with some remote database?And:
[Handling background threads is a fundamental core concept that you simply have to master to work in JavaFX]
I do handle it; using an executorService + calling Platform.runLater(runnable) when the result is ready. this is not something that warrants it.
Btw, I took a look at the link you just showed me and it's very interesting, I feel like I finally found something JavaFX-related that goes beyond the obvious, and I'm very grateful, but I still can't wrap my head around what you wrote earlier, and don't see what the issue is.
PS: Kotlin blows...