0

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?

micka190
  • 742
  • 2
  • 8
  • 20

2 Answers2

1

Constant class structure is among best practices

Keep your naming convention readable enough so that you won't require separate constant classes.

Always try to implement Keep It Simple Stupid and Don't Repeat Yourself

Related thread

What is the best way to implement constants in Java?

whoopdedoo
  • 2,815
  • 23
  • 46
  • Yeah, I saw that thread already. I may not be reading it properly, but it seems like they're more focused on the subject of whether or not creating interfaces for constants is a good idea (and the consensus seems to be "No.") – micka190 Sep 17 '17 at 13:19
  • @micka190 there also comparing vs enum, performance wise, just KISS & DRY, there are no best practices only good solutions for you problems that will be easy for future maintenance – whoopdedoo Sep 17 '17 at 13:26
0

In my opinion, everything is about maintenance and readability. To find out if something is beneficial or not I used to ask myself the following questions :

  1. Does this change will increase the readability and why?
  2. Am I going to make any further changes in the future or someone else? If so what should I do to make those changes easier?

Adding too many unnecessary constants will increase the size of the class and it will be more of a 'noise'. Now in your example above about the GUI constants, my opinion is more like 50-50. I like to do that also with my GUI constants but only if my class is small. Thus If the class body is big I used to remove the constants and to create a default constructor initializing them inside as constants and not as global variables, the only reason I do that is to increase readability because someone can easily fold the constructor's code and the noise disappeared.

Lastly, I believe the most important thing is what's it's better for you and your team. Even if you used to initialize them as global variables or something else when you work as a team you tend to adapt to the team's programing style choices and not to the "optimal" as you should.

JKostikiadis
  • 2,847
  • 2
  • 22
  • 34