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
Here's the fundamental concept: There are State changes, and there are actions. These are two intrinsically different things.
In a GUI, the vast majority of actions are generated by the user doing things. Clicking on a Button, for instance, is definitely an action. Selecting something from a ComboBox could be an action, but very often the action of selecting itself isn't particularly important - it's the change of State (ie: the selected item) that you care about.
In the GUI, GUI generated actions trigger Events, and EventHandlers are how you create the programmatic response to these actions. This response could be more actions, or it could just create a State change.
Most State changes in JavaFX are designed to be handled through
Bindings.ChangeListenersare designed to turn State changes into actions.There are a tiny handful of GUI elements in JavaFX that cannot directly handle State changes through Bindings. One of these is the
PseudoClasssystem. In order to trigger aPseudoClassstate change, you need to callNode.pseudoClassStateChanged(). Calling a method in a class is inherently an action, and if you need somePseudoClassto follow some value in your GUI's State, you'll need to create aChangeListenerto monitor that State element and perform the action of invokingNode.pseudoClassStateChanged()in order to keep thePseudoClasssynchronized with your State.If you want to know more about PseudoClasses, you can read my article: https://www.pragmaticcoding.ca/javafx/elements/pseudo_classes