aochagavia

aochagavia

Adapting to Elixir, coming from C# and Typescript

Two months ago I started working professionally on an Elixir project. I had no previous Elixir experience, but I am picking it up at a reasonable pace (previous experience with Rust, Haskell and non-blocking programming in general have proved very useful). One of the things that I find myself missing is the top-notch IDE support I used to have in C# and Typescript, which gave me an incredible productivity boost and made it possible to find my way in new codebases very quickly. Unfortunately, IDE support for Elixir is quite basic at the moment, and my productivity suffers from it (though the fact that I am still learning the language also accounts for the productivity hit). Did anyone here have to adapt to Elixir coming from statically typed languages with great IDE support? How did you adapt?

Some concrete things that boost my productivity as a programmer, which I find myself missing, are:

  1. Find all usages of functions, modules, struct fields, etc.
  2. Rename functions, modules, struct fields, etc and have their usages automatically updated.
  3. A strict compiler that catches type errors, enabling things like “type-error driven development”, where you change something and chase the compiler errors until everything is fixed (it is almost magical to fix all errors and see that the change you introduced “just works”!).

Without these features, I notice that I am much less confident about changing existing code, which I find problematic, because frequent refactoring is often necessary to keep a codebase healthy. Writing new code is not a problem, though.

For the time being, I am using plain textual search a lot (which doesn’t really cut it given the size of the codebase, but I know of nothing better at the moment), using @spec wherever it makes sense (it is not as nice as proper types, but it helps a bit) and writing tests to ensure everything is working properly (ExUnit is great).

By the way, I understand that many of these features are incredibly complex or even impossible to have in a language such as Elixir, so it is probably not a matter of waiting until the tooling gets better. That is why I am looking for ways to adapt my workflow as a programmer, since I assume I am not the first one to stumble upon these obstacles.

I’m looking forward to your suggestions and insights!

Most Liked

asianfilm

asianfilm

Consider writing your core business logic in Gleam because Elixir is not (and is likely never) going to give you the type-driven development experience of Haskell.

I have a modern 50,000 line Elixir codebase that uses best practices like Credo, Dialyzer and test suites for its core business logic and it never gives me enough refactoring confidence.

I’m considering moving its core business logic to Gleam, which has a lot of Elm influence, and some new IDE support in its latest release (but that aspect is also still a work-in-progress).

aochagavia

aochagavia

Thanks for your answers! I forgot to mention I am currently using vscode with the ElixirLS and Elixir Test extensions. I assume other IDEs offer a somewhat similar development experience (btw I tried IntelliJ a few months ago, but it didn’t work as smoothly).

Right now it looks like I will need to get used to basic IDE / compiler support and hope the features of the language compensate for that. Some that are already proving very useful are:

  1. Process as a first class citizen (I really like the idea of putting state in processes rather than objects, because you are forced to think up-front about supervision)
  2. Easy multi-node programming (I recently had the joy of using a replicated Nebulex cache; with just a few lines of code, everything magically worked, whereas in other language I would have needed to deploy an additional service like Redis)

My hope is that these and other benefits will in the end outweight the limitations caused by lack of static typing (and the great IDE they usually enable). Thanks to @AstonJ for pointing this out!

Now if anyone has any tricks to keep a codebase maintainable, I’d be glad to hear them (maybe there is a cheat sheet somewhere). One trick I am already applying is to use structs for GenServer state, and pattern match on the struct when handling messages, so you get compiler warnings if you try to access non-existent fields and get autocomplete support in the IDE.

wfgilman

wfgilman

For what it’s worth, in Jose’s 10yr talk he speaks about prioritizing the IDE experience to make Elixir more accessible and make the learning process easier. It’s toward the end of the presentation here Celebrating the 10 Years of Elixir | José Valim | ElixirConf EU 2022 - YouTube

bmitc

bmitc

Hi @aochagavia! I also came to Elixir from statically typed languages, one of which was F#. I am still struggling a bit with Elixir to a degree because with dynamic languages, there seem to be a wide variety of different styles of programming. For me, I find it unacceptable to have to read a function’s (and its subfunctions’) implementations to understand what it does. I want @typedoc and @spec and the function and argument names to tell me that. I don’t think you’ll necessarily find that in most Elixir codebases. However, many seemingly seem comfortable moving through such codebases and making changes. I’m simultaneously impressed but also suspicious, because I have found several bugs because of this in those codebases.

For me personally, I use Visual Studio Code with the ElixirLS extension (ElixirLS: Elixir support and debugger - Visual Studio Marketplace). The extension plus features of Visual Studio Code can help somewhat for your (1) and (2) but definitely not what you’re used to with Visual Studio (I presume). But as far as I know, it’s about as good as you’re going to get right now. For (3), ElixirLS will dynamically display type information, but it’s usually way too generic to be useful. I personally shoot for 100% coverage of typespecs (I see almost no reason not to) and then use Dialyzer. Dialyzer is not really a type checker. It’s sort of its own thing, but it’s as close as you’re going to get to help with this.

In Elixir, you can accomplish type-driven development, and this is definitely something I strive for. Use structs as much as possible and define @type t() :: ... for them so that you can use that type definition in modules that use your structure, including in the module that defines that struct with __MODULE__.t(). I also heavily use @type definitions to create domain-specific types that help make my code easier to read and understand, while also utilizing Dialyzer.

For the most part, I’d say 95% of code away from I/O and process/message boundaries in Elixir and Erlang does not need to be dynamic. So, I treat if it was statically typed via detailed @spec usage, clear argument names, custom @type definitions, and using Dialyzer.

This is certainly an opinionated stance, but I personally see no other way to write solid code that is easily read and maintained and can onboard new developers.

There is no fundamental reason why an advanced and modern IDE experience doesn’t exist yet. It’s just that Elixir is new and it hasn’t been taken on by somebody.

AstonJ

AstonJ

Welcome Adolfo!

Something we often tell people coming to Elixir from Ruby is that despite how it looks with the obvious Ruby influence on syntax, Elixir isn’t Ruby and there are some stark and fundamental differences.

So that’s something I would say here too - keep in mind Elixir isn’t C# or Typescript and while you may be leaving some things from those ecosystems behind, you will be gaining others (for instance, whatever the reasons might be why you have found yourself working with Elixir now).

With regards to working with Elixir itself, you might find these threads interesting:

Personally I think neovim is great, and if you want to see it in action (as well as Dialyzer) you may want to go through PragDave’s course:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement