I have something like:
:upload_path_changer module from options (
if not found in options, get it from the config file;
if not found in the config file, use
Do you think the code above can improve in readability?
Something like this?
Keyword.get(opts, :upload_path_changer) || Application.get_env(:file_uploader, :upload_path_changer) || MyApp.UploadPathChager
Just make a helper in one of your modules. I also found such snippets very tiresome and just hid the complexity away in neat quick functions.
This will change semantics of the original, as
opts = [upload_path_changer: nil] will return different results in original and this one.
Maybe merge options with defaults?
default_opts = [
upload_patch_changer: Application.get_env(:file_uploader, :upload_patch_changer, MyApp.UploadPatchChanger)
# other options if needed
opts = Keyword.merge(default_opts, opts)
I really like this approach, especially when there are several default options. ^^
Haven’t considered that case. Though I doubt that there will be a module
nil that happens to implement the behaviour…