I actually really don't like moving configuration out to a config file unless it actually is necessary. I also don't like feature flags except where actually necessary.
Yes this gives you more power to change things with config instead of code, but it means that from now on you have to treat those values as abstract quantities rather than as something you can actually reason about.
Prefer to put configuration near where it is used. Prefer to put utility functions near where they are used. A single page of code is your "cache" and accessing information in the same page is way faster than having to look elsewhere, and that's even if you already know where to look.
Obviously you need to make exceptions for things that genuinely need to be configured elsewhere, but if it doesn't need to be configured elsewhere, please just configure it right by where you use it. It makes debugging and understanding the code a lot easier.
Option 1:
sub truncate {
my ($self, $str, %opts) = @_;
my $max_length = $opts{max_length} // $self->max_length // get_optional_config('max_length') // 15;
return substr($str, 0, $max_length);
}
Option 2:
sub truncate {
my ($self, $str) = @_;
my $max_length = 15;
return substr($str, 0, $max_length);
}
In option 1 you have 3 different places to specify max_length (and you just know that $self->max_length is going to look in more than one place as well...). Trying to divine the actual behaviour of truncate() from this code is very difficult, and it gets worse for functions that do more complicated things and are configured by multiple interacting parameters.
In option 2 you know it truncates at 15 characters, no exceptions.
Yes this gives you more power to change things with config instead of code, but it means that from now on you have to treat those values as abstract quantities rather than as something you can actually reason about.
Prefer to put configuration near where it is used. Prefer to put utility functions near where they are used. A single page of code is your "cache" and accessing information in the same page is way faster than having to look elsewhere, and that's even if you already know where to look.
Obviously you need to make exceptions for things that genuinely need to be configured elsewhere, but if it doesn't need to be configured elsewhere, please just configure it right by where you use it. It makes debugging and understanding the code a lot easier.
Option 1:
Option 2: In option 1 you have 3 different places to specify max_length (and you just know that $self->max_length is going to look in more than one place as well...). Trying to divine the actual behaviour of truncate() from this code is very difficult, and it gets worse for functions that do more complicated things and are configured by multiple interacting parameters.In option 2 you know it truncates at 15 characters, no exceptions.