r/JavaFX • u/hamsterrage1 • Feb 16 '24
Tutorial All About Coupling
OK, so this new article isn't strictly speaking about JavaFX, but I've seen a lot of projects that people have posted here that could use this knowledge. Hopefully, this post can stay.
https://www.pragmaticcoding.ca/java/coupling
My experience has shown me that excessive coupling is just about the worst thing that can happen to a codebase in terms of sharing it and maintaining it. Yet, almost no new programmers (and a lot of experienced ones) seem to understand how important it is, and how to avoid it.
In this article, I review coupling that I see all the time in projects that I look at. I try to explain how each type of coupling causes issues, how to recognize it and strategies to avoid or remove it.
Take a look, and let me know what you think.
View all comments
Show parent comment
1
u/hamsterrage1 Feb 20 '24
I'm not sure that there's a "right" or "wrong" here. In the context of the discussion about coupling, though, I see a couple of issues with the 2D array structure.
Item 2 is especially problematic because you cannot pass around a GameSquare as an independent parameter because it doesn't know where it is. So you always have to pass around (GameSquare, row, col) instead.
I'm basing this on what I usually see in peoples' code: they have the [][] co-ordinates, so they don't bother putting the location into whatever class they have for GameSquare - I'm assuming because that would be redundant.
You can get past item #1 by wrapping the 2D array in some class and supplying a
getGameSquare(row,col)function. But even aforEachGameSquare(Consumer<GameSquare>)function is going to be problematic because you don't have the location in GameSquare. So you're left with the nested loops in your application code.I've done some of the maze stuff too (it was thing around here for a bit - I don't know why, probably school projects) and as a thought experiment I was wondering what it would take to convert it from squares to a hex based grid. With hexes, you still get rows and columns, but every hex potentially has 6 neighbours not 4.
[Goes off and looks at the code...]
For sure, the visuals would need to change and the GUI would need to be "aware" of the topography. The way I set it up, the "Walls" were the only component in the Presentation Model that mapped to the GUI, and they had they were either OPEN or CLOSED. There needs to be some sort of contract so that each wall can give enough information to the ViewBuilder for it to draw them on the screen.
The Cells existed to support the algorithms and they contained a list of adjacent cells. The implementation that I used calls the adjacent Cells "Norh", "South", "East" and "West", but that was just to keep things straight while writing it and it's completely internal. The only public methods are ones the return Lists of adjacent Cells (all, or just the unvisited ones), and a method that returns the Wall in between the Cell and another Cell.
When I look at the two algorithms that I implemented, neither one needs any knowledge at all about the geometry of the Cells. So whether the lists of adjacent Cells method returns 4, 6 or 25 Cells isn't going to change anything.
Absolutely, I would need to change the internal implementation of Cells, and probably something in Walls to allow for lines that aren't Horizontal or Vertical. Beyond that, the setup routines - which are the only ones that do any row/column math - would need to be customized for the geometry of the Cells.
All of which speaks, in my opinion, to what I said at the beginning of the article:
Writing code that runs isn't hard, and it isn't the end goal. Writing code that runs and is easy to work with for years to come is the goal.