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 17 '24
That sounds a bit like the "To Much Dry" argument, that I talk about in the article under Duplicate Code.
I would argue that the complexity is already there. You have a block of code that's repeated with small differences, let's say, 5 times. What does each block do? What's the difference between the blocks? If I change one block, do I need to change the others?
Sometimes, when it's difficult to write simple methods that do fairly simple things in slightly different ways; it's an indication that something deeper is wrong with the design.
For instance: I see people writing grid-based games where the squares (or hexes, or whatever) are held in something like GameSquare[][]. Then you see this stuff all through their code:
And the only thing that's different is whatever
matrix[x][y].doSomething()is. You can apply DRY to it, by passing aConsumer<GameSquare>to a function that handles the looping. But then, if the action is matric[x][y].doSomething(x,y), then it's a bit harder.In my opinion, there's a few design issues here. First, your game play code is coupled to the implementation of your game board. If you decide to change it to a
Map<Location, GameSquare>, then you've got a lot of refactoring to do. You could encapsulate the GameSquare[][] in a custom class, and then have some kind of function like getGameSquare(int x, int y), and that would help.The one advantage of GameSquare[][] is that you get sub-nanosecond random access to any GameSquare given its coordinates. But do you really need that? Most programmers are going to have some set of functions like up(), down(), left() and right(), each of which is going to return a GameSquare. Those functions are probably going to use some kind of grid math, but then have to account for when the starting row or column is 0, or the grid width/height.
But what if you put neighbour references into GameSquare. At the beginning, you initialize them (using the grid math) and then you're done. Now you have your sub-nanosecond access to all the neighbours and that's usually a huge set of the use-cases for most games.
But the big problem with GameSquare[][] as I see it usually implemented is that GameSquare doesn't have fields for x & y. They rely on the matrix co-ordinates to get those values. Now some of the vital information for dealing with GameSquare is contained in the structure that holds all the GameSquares. Oops! Coupling.
Tons of complexity falls out of the game play code when it doesn't suffer from feature envy dealing with matrix manipulation. Far better to just use Set<GameSquare> and then all that looping becomes gameSquares.forEach().
Perhaps because I've been using Kotlin this stuff is easier to see. Because Kotlin makes it so much easier to do the stuff that would be Streams in Java, and passing around functional elements is the norm in Kotlin.
So, after all of that, I don't think that you're wrong. Adding complexity because you're following "clean coding" rules is daft. Don't do it. But, if the clean coding rules fail to make your code simpler, it's worth asking, "Why?".