❔ reduce these magic number code smells

Hey there, i have the following code which looks quite identical. How can i clean the magic numbers to remove the code smells?
13 Replies
Omnissiah
Omnissiah2y ago
is the question like "what name should i give them?"
electronic heartbreak.
No, what I try to ask is; Since these are all the same over the 3 functions I want to implement something so that these magic numbers can be removed or are like defined once.
Omnissiah
Omnissiah2y ago
it all depends on your needs they could be constants in the class they could be methods from a service if they need to be calculated in some way they could be static properties of a class if they need to be used in multiple places
electronic heartbreak.
I would need to make 4 additional constants then, but still, needing to put them on all 3 functions feels still a bit "messy"
cap5lut
cap5lut2y ago
that looks a lot like u want a factory method to create the clusters
electronic heartbreak.
this code is already in 1 builder tho,
cap5lut
cap5lut2y ago
well, make it a private helper method in the builder or so another way would be to define something like a rectangle or region struct and store these somewhere / make factory methods for each and then use these to pass to the data to the different GetSub... methods
nukleer bomb
nukleer bomb2y ago
const int Width = 6; then
6 -> Width
3 -> Width / 2
2 -> 2
6 -> Width
3 -> Width / 2
2 -> 2
But I don't know the context, so it can be wrong
Omnissiah
Omnissiah2y ago
it seems to be like a margin?
electronic heartbreak.
no,. i need to build sudokus, and I need to build a samurai sudoku too but a samurai is very different compared to a regular one
M B V R K
M B V R K2y ago
It depends on your needs, but in general the first advice is use Constants
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server