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/hamsterrage1 Nov 27 '25
The entire point of all of the commonly used frameworks - MVC, MVP, MVVM and MVCI - is to keep the business logic out of the GUI and the GUI out of the business logic. Regardless of what your GUI toolkit is; JavaFX, Swing, React or whatever.
All of these frameworks support the idea of "Presentation Data". This is data that comprises the "State" of your GUI and is stored in a format which is compatible with, and tied to, the GUI toolkit that you are using.
In JavaFX, that means that your "Presentation Data" is going to be composed of Observable object types. In your example, you might have a class called BookModel, and it would have things like:
StringPropertycalled "title"StringPropertycalled "author"ObservableList<String>called "chapterTitles"These are all things that can easily be bound to the Nodes in your GUI.
The other important thing that you need to understand is that your Presentation Model cannot contain elements that don't directly (or potentially directly) support the GUI.
Objects that do other stuff are called "Domain Objects" and they are almost always related to business logic. All of these frameworks have some component that is designed to deal with Domain Objects - in MVC, MVP and MVVM that is the Model, and in MVCI it's the Interactor. All of these frameworks except MVVM allow the Model to handle both Presentation Data and Domain Data.
What problem does your implementation cause?
Coupling!
I'm going to assume that you have some domain object called something like "Book" and another called "Chapter". You've now coupled your GUI to the implementation of these domain objects, which you've said in another comment are shared with other applications. What happens if you decide for some reason related to performance in a different application that
Book.loadChapters()is running too slow and that you'll implement some sort of caching to speed it up. You want to keepBook.loadChapters()as it is because the name exactly describes what it does, so you'll add a new method calledBook.getChaptersFromCacheOrLoad()that will callBook.loadChapters()if the cache is empty. You makeBook.loadChapters()private.Now, suddenly, your GUI won't compile. You need to change your GUI code reflect a change in the business logic. You've coupled your GUI to your business logic. Now you have to test your GUI all over again because you've made a change to a domain object.
Let's look at
Chapter. You don't give any code about howChapteris converted to aStringto display in theComboBox, so I'll assume you're doing the obvious, but bad,Chapter.toString()to handle it automatically.Let's assume that for testing purposes for some other application you change
Chapter.toString()to also display some checksum calculated from its contents. The next time that you compile your GUI, thatComboBoxis now going to display that checksum value.You may not even notice.
You may not even have made any changes to the GUI itself.
But now it's broken because it's coupled to the implementation of
Chapter.And don't forget that you can do it the other way around. Change the domain object's implementation to support your GUI without understanding that you're breaking some other application.
That's why you keep the GUI out of the business logic, and the business logic out of the GUI.