|
|
|
|
|
by danso
4472 days ago
|
|
Right...but even in this trivial gem, there may be the need to configure more than one parameter. And by having Configuration be it's own object, you can encode some validation logic at the configuration stage. I do agree that the constructor should have the option of passing in a Hash, which is then passed directly to the Configuration option. |
|
If you have this in many different places:
You don't have the information of how many numbers you are getting back. That's not a big deal, but adds to the cognitive load (or requires some comments). Also, if you need to draw different numbers in several different places, you will have to change the configuration many times: And as soon as you do that, you may need to take multi-threading into account, because you are mutating the class.Extrapolating, it is like defining the size of an array:
If instead you configure Array.new to have a given size for all instantiations, you also lose locality and you may run into thread safety issues.In the case of MegaLotto::Drawing.new needing multiple configuration options, you can use keyword arguments. If you need too many arguments, maybe the abstraction is wrong. Even if you want to move forward with too many arguments, you can add getters/setters to the newly created instance:
But this is not optimal design given the elements we have.