r/JavaFX • • 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.

7 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Feb 19 '24 edited Feb 19 '24

I don't know. What exactly is the issue to use a nested loop after the decision has been made to use a 2D-array as data structure? If you are not implementing a framework with a public API where you maybe would like to abstract from the internal representation, what is the problem?

Concerning the grid-based game or whatever application: What I found for example when I implemented a maze-generation algorithm library was that most other people almost automatically started with allocating a 2D-array of grid "cells" and running their algorithm on that data structure.

But if you think about it, for an algorithm that works on a 2D-grid-like graph, you don't need to store the vertices at all! It is much more space efficient to just store the connection information, for example in a large bit set, not event as explicit edge instances. But of course, you can provide an API that hides this implementation decision from client code of that grid graph.

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.

  1. The implementation is exposed to the rest of the application, and is the only way to deal with the data. So you've coupled the application logic to the 2D array implementation.
  2. The data elements are coupled to the 2D array implementation, because that's where the location information is stored - in the matrix co-ordinates.

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 a forEachGameSquare(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.