#I'm not sure where to store this magic numbers can you help me ?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
`someClass1.java
private static final double SCORE_A = 2.5;
private static final double SCORE_B = 3.5;
private static final double SCORE_C = 4.0;
private static final double SCORE_D = 5.5;
private static final double SCORE_E = 6.0;
private static final double SCORE_F = 7.0;
private static final double SCORE_G = 7.5;
private static final double SCORE_H = 8.0;
private static final double SCORE_I = 9.0;
private static final double SCORE_J = 10.0;
private static final double SCORE_K = 12.0;
private static final double SCORE_L = 12.5;
private static final double SCORE_M = 15.0;
private static final double SCORE_N = 16.0;
private static final double SCORE_O = 18.0;
private static final double SCORE_P = 20.0;
private static final double SCORE_Q = 22.0;
private static final double SCORE_R = 25.0;
E_H)
);`
And more and more al in someClass1.java
Like this i have 2 classes lets say someClass1 and someClass2
Yeah i have styling issues
In total i have around 50~ lines like this and actually i don't like it but i don't want to make dummy refactoring
I can help you
me personally i'd keep it in the same class but that's just my opinion 
then do help, here in the chat please
no luring into DMs 👍
Hi @lyric creek I think the issues are not scalable, hard to maintain and looks messy.
so in my opinion,
private static final Map<String, Double> SCORES = Map.of(
"A", 2.5,
"B", 3.5,
"C", 4.0
// ...
);
double score = SCORES.get("A");
Enums
@lyric creek
there many way based on the scnario
- if they used globaly use utilty class
- use nested class if it needed for some orgnization of the code
- use enum with field that hold the value
4)for general use on one class do as you did
but if your calss has 50 field that mean that your class may doing more than it should be
also check if it fine to put them on config file and use that file(properties file)
Scores are increasing so it looks like you could just create a math function that spits out a score based on a input (a = 1, b=2, etc).
this
public enum Scores {
A(2.5), B(3.5), C(4.0), D(5.5),...
private final int value;
private Scores(double score) {
value = score;
}
public getValue() {
return value;
}
}
when you have like a few constants you want to access everywhere but if you have many scores increasing in a pattern then what @tired vale said
Also if your inputs are linear (a=1, b=2, etc) then just an array of values will do as well.
double[] scores = {2.5, 3.5, 4.0, ....};
No we are avoinding using nested classes
Iam using this already yes but its still smelly
Nope it's just calculations i did separated the logic as it should be ... no GOD classes
Yeah i was already thinking about putting all in ENUMS but this is used only for single use
Yeah we are doing this already but i'm not gonna create new .properties file for such a case
If iwould use Enums it would be Enum nightmare really
then you could do
public enum Score {
A(2.5),
B(3.5);
private final double factor;
Score(double factor) {
this.factor = factor;
}
public double apply(double base) {
return base * factor;
}
}
double result = Score.A.apply(base);
@lyric creek then check what other suggested 🙂
Thank you all for suggestions
I'm just curious about your project.
I want to collaborate with you.
looking forward to speaking with you soon.
simply apply the private access modifier to the data
@rich escarp ?
if u have an integer: int x = 123; in the Main class, use this instead: private int x = 123;. this in theory makes the int unaccesable in other classes and files. if you still want to get it in another class, you need to use a getter, wich is a bit more complicated concept
(i just read the conversation sry)
Nvm its all good
btw why r u using doubles there?
