#I'm not sure where to store this magic numbers can you help me ?

1 messages · Page 1 of 1 (latest)

lyric creek
#

Hello there, i'm working on a project and i'm implementing an important calculations and i need to know where would you store stuffs like i have if we should not access them from a different class or keep them where they are.

queen pebbleBOT
#

<@&987246399047479336> please have a look, thanks.

lyric creek
#

`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

gleaming eagle
#

hi @lyric creek

#

do you need help?

lyric creek
#

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

gleaming eagle
#

I can help you

knotty maple
#

me personally i'd keep it in the same class but that's just my opinion peepo_heart

languid marten
#

no luring into DMs 👍

stuck raptor
#

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");

wild tulip
#

@lyric creek

there many way based on the scnario

  1. if they used globaly use utilty class
  2. use nested class if it needed for some orgnization of the code
  3. 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)

tired vale
#

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).

torpid sage
#
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

tired vale
#

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, ....};

lyric creek
#

No we are avoinding using nested classes

lyric creek
lyric creek
#

Yeah i was already thinking about putting all in ENUMS but this is used only for single use

lyric creek
#

If iwould use Enums it would be Enum nightmare really

stuck raptor
wild tulip
#

@lyric creek then check what other suggested 🙂

lyric creek
#

Thank you all for suggestions

stuck raptor
#

I'm just curious about your project.

#

I want to collaborate with you.
looking forward to speaking with you soon.

rich escarp
#

simply apply the private access modifier to the data

lyric creek
#

@rich escarp ?

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

lyric creek
#

Bro

rich escarp
#

(i just read the conversation sry)

lyric creek
#

Nvm its all good

rich escarp
#

btw why r u using doubles there?

lyric creek
#

Its a percentile equation’s

#

We are counting if the customer pass our questionnaire and if he can go forward