krp
Abbreviating function names, defdelgate, macros and MUMPS
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.
Most Liked
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?
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.







