krp
After a year with Elixir I grew tired of writing, reading, and thinking in long, core function calls that I used to the point they felt like boiler plate. This is not Elixir specific, but here it seems easy to create a solution. Recently, I’ve been making modules and functions with abbreviated names (often single letters) for core functions or slight modifications of them.
For example, from my module S
def s(string, splitter), do: String.split(string, splitter)`
def st(string), do: String.split(string, "\t")
Today I learned about defdelegate which would work for the straight alias S.s, but not for creating something like S.st. This has me asking a few questions.
- What are the advantages of using a macro to generate an “alias” like those described. In my readings it seems that creation of a DSL is benefits from macros. I seem to have missed why macros are preferred over functions (I’m sure this is basic) ?
- Without digging in and picking it apart, I can’t follow exactly what is happening in
defdelgate. Other than creating an alias function what is going on ? - Off the topic of Elixir, but related to abbreviated module/functions names: Are there any languages other than MUMPS that have these built in?
Thanks to all.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
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
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 working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
Some thoughts:
defdelegate/2can define delegates for bothS.s/2andS.st/1- unless I’ve missed something else in your expectations?defdelegatge/2is just creating a function that calls the target function. Its a macro so that interpreting the arguments is done once at compile time and not every time at runtime.gregvaughn
What you’re doing is optimizing your code for writing at the expense of reading, but most code is read many more times than it is written, so it’s a bad tradeoff in the long term. I would not want to join your team and have to maintain this code you’ve written.
In order to optimize writing speed, you should look into custom snippets in your editor of choice. If your current editor can’t do that, then switch to one that does and you’ll be able to train your finger muscle memory and reuse that for any language you write.
That being said, I do have a utility module in my work codebase that offers shortcuts to look up our core domain entities. We don’t go so far as single letter function names, and this is not used by business logic itself. It’s useful for troubleshooting in iex.
Exadra37
I totally second this.
One of the things that really pisses me off in Software Development is when I have to read code that uses abbreviations, because it’s not readable, I need to lookup what all that abbreviations mean each time I read the code. It’s just a pure waste of my time and energy.
In my opinion code should just look as much as possible as reading a book.
So, I have a question for you:
How you gonna track in your memory all this short-names when you arrive to a lot of them, like more then 20/50/100 or whatever is the limit for your brain to memorize them?
krp
Happy New Year kip and thanks for engaging
Regarding #1 I am able to get S.sn to work using defdelegate if I add a “dummy” variable with a default
iex(1)> defmodule S do...(1)> defdelegate sn(text, spl \\\ "\n"), to: String, as: :split...(1)> endiex(2)> S.sn("foo\nbar")[“foo”, “bar”]`
However, that seems a bit cloogey. What I would prefer to do is
iex(3)> defmodule S do...(3)> defdelegate sn(text, "\n"), to: String, as: :split...(3)> end** (ArgumentError) defdelegate/2 only accepts function parameters, got:"\n"...Regarding #2
That was my assumption, but when I benchmarked the aliasing functions above vs using a macro I saw no significant difference. However, just now as I’ve been writing, it occurs to me that benchee likely compiles each test prior to running.
Perhaps I should write a modified defdelegate. Grokking the original would be good in itself.
Thanks again and all my best.
krp
Happy New Year Greg and exadra37. Thank you for your replies. They are clearly thoughtful.
I was hesitant to even begin to go down this path. Your points were all ones I considered and frankly fretted over. In the past I’ve run groups where I was dogmatic about explicitness. Finally I felt comfortable based on the following
I (or anyone worth their salt) could throw together a script to expand my abbreviated forms to the originals in short order if it was ever necessary. Given my domain that is unlikely and once I have the macro discussed with kip it should be even simpler.
My editor allows me to view associated @doc fields with a simple mouse over of the function. So if in the rare case I question what S.st or S.sn etc mean it is right there.
Most importantly, I saw that the core functions were acting as a barrier for my reading and thinking about code.
String.split(term, "\t")seems to require more of my brain thanS.st(term)and certainly more than|> S.stFor example
iex(1)> S.r("foo_x", "_x") |> S.c()"Foo"rather than
iex(1)> String.replace("foo_x", "_x", "")|> Macro.camelize()"Foo"The majority of the second is essentially boiler plate. In the first what makes this code different stands out. I am also taking advantage of having a S.r/2 that blanks what is being replaced. Perhaps best of all I was able to forget that for some reason
camelizeisMacro.camelizein elixirMnemonics: The first letter is that of the core module. The second is usually for the function. Additional ones tend to be for specific uses of the function. Also I’ve worked to be consistent so that S.st and E.jt are split by and join by tab, respectively. Currently I have ~125. A few I’ve deprecated as capricious as I don’t use them enough to remember them.
In the end it likely comes down to domain. If I were running a group that needed to slot in elixir programmers with no additional training, or was writing for a client that needed the same, this would likely be another discussion.