Edit: I've seen this thread: What is the best way to implement constants in Java? already. They seem to be focusing on whether you should be using an interface with constants. I want to know where I should be placing my constants if they aren't being used by the entire class.
If I'm designing a GUI made with the Shape package (javafx.scene.shape.[whatever]), how should I go about placing my constant variables used to size them?
Class-scope
I was taught that constants should always be at the top of the class, which would give me something like this:
public class Foo extends Group{
private final double WIDTH = 100;
private final double HEIGHT = 100;
private final double PADDING = 10;
...
}
Granted, I would probably put my public variables, if any, over those. I'd probably also put the non-constant privates as well.
However, this means I have these small blobs of constants at the top of my functions. I have two problems with this:
1 - They are often ignored
Robert C. Martin talks about how sometimes programmers will comment in "noisy" comments. These comments are so useless that they are ignored by the reader eventually. I feel like this also applies to blobs of constants used for measurements and positions.
2 - They aren't being used by most functions
Quoting Mr. Martin, this time:
Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of those variables. In general the more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive.
He later says that, of course, it's pretty hard to reach "maximal cohesion", and that's okay as long as the class is as cohesive as possible.
But wouldn't constants used for measurements break this cohesion if they were instanced by the class itself instead of by its functions?
Function-scope
Instead of instancing them with the class, should I just have them be function variables like this:
public class foo {
private void initGUI() {
final double WIDTH = 100;
final double HEIGHT = 100;
final double PADDING = 10;
...
}
}
Doing it like this goes against what I was taught, however (but my teachers have been proven wrong in the past on some things).
Some people have told me to make a "Measurements" class and put all my constants there as plubic static
variables. However I'm against that, as that would mean I'm dependent on that class every time I wanted to add a measurement and that I would have a class full of unrelated variables.
So, what's the best practice (or standard) to follow when handling constants used for measurements in JavaFX?