amirOrbe
Hi everyone, I have a function with two parameters with default values something like:
def(param1 \\ %{}, param2 \\ "") do
###
end
this is the best practice for do that? or you know another way ?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 17 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
Oh, I am not against keyword lists as options, of course. That’s one of the gems in Elixir: it allows you to emulate named function arguments and it is super convenient!
My objection is towards more than one optional function argument (i.e. 2 or more having default values). That becomes messy super quickly.
SoldierCoder
I hate to even write any of this post because I fear it can be taken the wrong way. I do understand that you made a particular recommendation but I still have no idea why. What’s the downside of using an opts list? Can you give some concrete examples that show why its a bad idea?
FlyingNoodle
There is a good chance where even your own application code becomes so large that changing all call sites would produce pull requests that touch hundreds of files spanning multiple teams.
So your advice also holds for application code I think and not just libraries.
dimitarvp
Agreed 100%. Indeed the emphasized part IMO goes without saying. Hence my original statement didn’t include it.
Plus, @derek-zhou, more than one default argument is something that I don’t remember the last time that I needed it.
…plus you can now use treasures like
Keyword.validate!orNimbleOptions.zachdaniel
Sure, there is always the caveat of not breaking existing code. But “never do it if you can avoid it” I think is always implicit in this kind of advice. I don’t see how in
top_n/4the fourth arg only makes sense with the 3rd arg. Those both look like things I’d want to be able to specify independently.I think for libraries who have to avoid breaking changes, you should begin with options lists over even a single optional argument, so that you never have this problem. In our own application code it’s a different story as we can update all callers when we change the function. Even still, all cases where I used to reach for optional args I now instead use options lists. All of the benefits, none of the downsides.
derek-zhou
Of course you can achieve that with keyword list. However, multiple default arguments still has its place:
Enum.min/3code-shoily
Totally agree. I made a whole mess by having more than one default argument (that too booleans, those are the worst)
garrison
You know, it really bothers me knowing that KW lists require O(n) iteration over the list to extract the argument. I know full well that it will (almost) never matter in practice and I’m pretty sure it’s actually pushed down to native code, but I wince every time anyway…
Of course, I still use them frequently because named args can’t be beat for readability when you have a lot of them.
D4no0
I personally have found default arguments to be a good tool for trolling, as the default arguments are not restricted to the last arguments only as in many other languages, hence this is valid code too:
zachdaniel
I don’t see why you’d need multiple default arguments for that? You can detect presence with keyword lists and derive defaults etc.