mercredi 26 mai 2021

adding argument via constructor or method

I have these validators classes

interface Validator {
     bool Validate(string value,object options)
}

class NumberValidator extends Validator {}
class EmailValidator extends Validator {}
class RangeValidator extends Validator {}
...

as you can see that the options are passed on the call to the Validate method. but I was wondering if it's better to have the options argument to be passed via constructor instead of the Validate method? (from CleanCode perspective).

so the interface and my implementation classes will be like this:

interface Validator {
    bool Validate(string value)
}

class SomeValidator extends Validator {
     private object options;
     public SomeValidator(object options) {...}
     public bool Validate(string value) {...}
}

...

I sense that having the options on the constructor will be better, but honestly, I don't have a reason why it's better.

Aucun commentaire:

Enregistrer un commentaire