Should I choose Elixir for web apps?

Hi, I’m having trouble deciding on whether to use Elixir as my main programming language for building web apps which usually won’t involve much of a need for concurrency.

I would prefer a language that is fun, productive, and that is easy to scale. Elixir seems to check all these boxes better than any other language out there but there’s just one thing that’s holding me back.

I would prefer to use an OOP language in order to further my OOP skills in case I want to land a job or build a team (both easier with OOP).

I’ve been learning Elixir for a few weeks now and it seems to be mostly functional from what I’ve learned but I’ve also read about how it’s (arguably) The Most Object-Oriented Language.

The object oriented nature of Elixir appears to mostly be related to the fact that all Elixir code runs inside of isolated processes which communicate via messages. So it appears to be functional on the dev side of things and OOP on the runtime (correct me if I’m wrong).

Should I be concerned about using a Functional language for most of my work? I read so many developers on Quora saying that OOP is the only way to go for building apps and that Functional programming is awful for real world applications. Perhaps they are confusing impure functional languages like Elixir and Clojure with a pure functional language like Haskell, not sure. Elixir does appear to be a lot more useful for building non-trivial web apps than other functional languages so I’m kind of confused as to what I should do.

The only other languages which are meeting my criteria are TypeScript, Go, and Python. I find Elixir to be more fun than those three but Python is harder to scale than Elixir from what I’ve heard.

What would you do in this situation?

1 Like

I’ve not been using Elixir for a long enough time to have a thorough answer to your questions, but if the only other languages you are ready to use for web apps are Go and Python, then it’s a no brainer : use Elixir.

If you want more OOP and an nice yet enterprisey language, then I’d advise C#. I used to enjoy working with that for a big intranet website.

2 Likes

Elixir is fully functional, but it’s possible to create a OOP-like API. For a good example check this library:

Since it’s more like than actually OOP it probably does not have any compiler optimizations which are typical for OOP languages. Of course there are also probably used other optimizations, but only for functional programming (Erlang/vm optimizations).

In short Elixir is based on Erlang which used BEAM (virtual machine). This allows to create your own processes (in BEAM) even if you have started app once. Processes in BEAM are much more lightweight than OS processes which means you one app can easily create thousands and even more processes (again in BEAM VM). Each process can store its data which can be modified, but

In OOP languages you are working on exactly same data. In Elixir you are working on data copies. So when you change data you are actually getting its modified copy rather than modified data itself (it matters in memory). What process is doing is basically replacing old data with its modified copy.

# old data
data = %{name: "example"}
# store old data
store_in_process(data)
change_data_in_process(name, "sample")

defp change_data_in_process(key, value) do
  # old data
  data = get_data_from_process()
  # store modified copy
  data |> Map.replace!(key, value) |> store_in_process()
end

Of course such code would not work - it’s just to visualize how its working inside.

For more information take a look at:


Especially in Advanced group you have some info about concurrency.

1 Like

Thanks for the reply. I just now edited my answer as I forgot to add TypeScript to the languages that meet my criteria. I actually have used C# a bit and I think it’s a great language but I wanted something a bit more light weight than Java and C#. Also, I would only want to use servers with Linux which cancels the idea of C# since .Net Core isn’t anywhere close to where it needs to be for me to consider using it yet.

I’ve seen that OOP library before and the associated talk he gave and I thought he was just trolling so I didn’t consider using it. It does look interesting though if only food for thought.

Also, thanks for sharing those actor model resources. They really helped me gain a deeper understanding of the Actor Model and how useful and awesome it is. I’m going to continue learning Elixir and I’ll try to learn more by building out an app. I don’t have any previous experience with a functional language so Elixir has been a bit of a puzzle to me but it’s quite interesting and fun and I’m starting to get how it all works.

Well … it’s similar change like Windows to Linux with “weird console”. It’s definitely change of habits, but you would find it really easy to learn soon. Comparing to other languages you do not need to go around to solve specific problem, because lots of really helpful core functions and libraries. Also documentation looks best for me here. There is almost no magic here. Even Phoenix which is considered to be framework is basically just typical library with some easy to understand meta-programming (__using__/1 macro and use … calls).

I don’t believe that we have “trolls” in our community. To be honest Elixir could look like onion …

which then could be similar to ogres:

but … I don’t believe there are “trolls” here, because we have really nice and kind community (thanks to our forum supervisors). :077:

For sure, by “trolls” I mean people which could confuse others about how Elixir is working. One time I tried to troll some people, but it was more just for 1st April (which at end I said about) and it was intended to force think why it gives error rather than confuse newbies. :smiley:

Short answer: If you enjoy it.

Longer answer: It doesn’t sound like you actually need any language specific features for what you’re doing so try each of your candidates for a bit and see which you enjoy most and go with that. If you’re specifically looking to learn something that will lead to a job then check local job ads to see what companies near you are using most and learn that.

Sounds like you’ve already made up your mind.

  • Just because it is written in an OO language doesn’t actually mean it’s OO.
  • Good OO isn’t easy.
  • OO as it is practiced today is primarly based on the assumption of unshared mutable data and as such is addicted to mutability.

Thinking Outside the Synchronisation Quadrant - Kevlin Henney [ACCU 2017]

When you are only used to manipulating the flow of control and mutating values in place with statements then it can be a bit difficult to get into the mindset of transforming data and controlling the flow of data with expressions instead.

Whichever way you turn, know what you are getting into.


https://elixirforum.com/t/elixir-from-java-guy-perspective/3373


3 Likes

.NET Core is cross-platform, I write ASP.NET Core apps on Arch Linux, just install dotnet, mono and MSBuild and you’re ready to go. It doesn’t have as good tooling as in Windows, but still enough for serious development.

I read so many developers on Quora saying that OOP is the only way to go for building apps and that Functional programming is awful for real world applications.

Read a PHP forum and they’ll still say PHP is cool lol. /jk

Nowadays almost every programming language implements functional features like keywords or libraries that mimics some behaviors. I’d say functional programming is trending topic right now, as it makes code more concise, and helps the programmer by reducing side-effects.

Either Elixir or C# are really good options for web development.

Spoiler

PHP too, but don’t say it to anyone.

1 Like

Elixir is a great language for Web Development, and it would be my choice if everything else was equal. That said, it’s not an OO language, and try to use it like that will have you fighting against its strengths.

Have you thought about Ruby (and Rails)? It’s an object-oriented language with a syntax similar to Elixir. Actually, it’s really the other way around but that’s another story.

Ruby doesn’t scale as well as Elixir, but neither does Go or Phyton. A lot of Elixir developers (including its author) were Ruby developers first. The end result is that you can find lots of tutorials and examples that help you with the transition. Also, the new version of Ruby supposed to improve performance and scalability.

I understand Rails makes it a breeze to get a new app up and running. If you learn both languages, you could use Ruby for rapid prototyping, and Elixir for the real system. Or, even use Ruby in production until you really need to bring Elixir into the picture.

Whatever you decide to do, choose a language that you enjoy working with. Good luck

6 Likes

A web app, by nature, is highly concurrent. Sure, you may not need to write your own concurrent code in your application. But by using e.g. Phoenix you would be reaping the benefits of Elixir’s concurrency, whether you realize it at first or not.

I would echo this important point.

4 Likes

All in all it depends on your definition of “Object Oriented”

Alan Kay, the person who is credited with coined the term “Object Oriented Programming” described it as a programming model with lots of independent processing units that maintained their own data and could communicate with one another through message passing.

That is the very essence of the Erlang (and by extension, Elixir) programming model. The units, independent in memory and compute, are known as Processes and they can communicate using message passing. This aligns very closely with the model that Alan Kay described. (He has since lamented the term saying if he had to do so again he might call it “Message Oriented” - to increase the emphasis on the separation of the processing units).

“Object Oriented Languages” grew from this concept to describe a language which sets aside a small piece of memory, of “state”, and controls access to that memory through language constructs (Encapsulation). The paradigm grew to include concepts like Inheritance (or prototyping) to allow for code reuse. And Polymorphism to help a developer apply problems solved in the abstract to concrete solutions. The task of learning “Object Oriented Programming” then, is focused on learning and applying these concepts - understanding how to use Encapsulation, Inheritance, and Polymorphism to solve problems. While these are good concepts - they are not fundamental to the “pure” concept that Alan Kay described.

If your goal is to learn those concepts, and how to work with them effectively, so that you can use any “Object Oriented Language” (C++, C#, Java, JavaScript, Objective-C, Swift, etc.) then learning Elixir is probably not the best vehicle to study. In some sense this the “practical” definition of “Object Oriented” in the modern lexicon.

If you are looking for an environment that is much closer to the “pure” concept of “Object Oriented Programming” as described by the person that coined the term then the BEAM and associated languages, including Elixir, may be your best bet.

5 Likes

As @felideon said, Elixir (and by virtue, Phoenix) are highly concurrent – and all web apps are highly concurrent as well.

Also, “we will only ever serve like 10 requests a minute” are famous last words.

Phoenix will delay your scaling troubles for much later. Even only for that it’s very much worth it to use Phoenix.

1 Like

I think the users on Quora are most likely talking about languages like Haskell when they are advocating against functional languages for web development. I’d also like to make a (possibly controversial) point about syntax. When someone says “functional programming language”, the languages that probably come up in the listener’s mind are Haskell, Lisp, and Clojure (and possibly Elixir and Erlang of course). Out of those, the syntax to all of them except Elixir is far enough away from the common algol-derived programming languages (e.g. C, Java, Javascript) to feel a serious amount of alien-ness. I think that the basic familiarity of the Elixir syntax (which in part derives from the influence of Ruby) is a major benefit of Elixir over other functional programming languages.

The bottom line is that Elixir is an excellent language for web applications (IMHO) and it is a relatively easy language to pick up for newcomers. You still have to deal with learning about the functional-style and immutability, but you can do that without worrying about type classes and monads (or a completely foreign syntax).

7 Likes

Typescript is nice, but if you want to code the server in that you are going to have to use nodejs, and I think that again Elixir is a better alternative. What you should do in my opinion, is setup an Elixir project with typescript support, so that all client code you need can be written in that. You should be able to think about some nice interfaces that would require the use of advanced OO concepts so you can get your OO experience.

Personally, I have written web app servers in JS, C#, PHP, OCaml and am starting with Elixir. I only enjoyed C# and Elixir for that task, and you should probably take that with a grain of salt but I would say that OO (in the sense of inheritance, polymorphism, …) doesn’t add much (if at all) to the web stack. The functional way the plugs work with Elixir/Phoenix is so much clearer than anything I’ve seen before.

3 Likes

This is very likely off-topic – but would you be so kind as to share what didn’t you like about OCaml?

I am considering learning OCaml (and up my level with Go) and I am gathering any and all feedback.

1 Like

I started OCaml recently, via Bucklescript/ReasonML. Syntax is nice in the inside.

But as a newcomer in the OCaml world, I find it difficult when dealing with the outside world… lots of coding/decoding, for exemple JSON to OCaml records…

And FFI is just painful to learn, lots of code annotations.

I am trying to wrap Phoenix js and it looks like this, just to translate one function.

  [@bs.module "phoenix"][@bs.new]
  external init : (endPoint, ~opts: Js.t('opts)=?, unit) => t = "Socket";
  let init = (~opts: option(Js.t('opts))=?, path) => init(path, ~opts?, ());

I am not familiar with the syntax, but I find it ugly.

Ok, I wrote my response a bit too soon. It seems that you kind of meta-trolled me and I am too tired to be picking up on things like that right now. Normally I’d notice right away. Sorry about that :smile:

You think that response with ogre holding onion on image could be serious? :077:
Of course I was just joking. :smiley:

There’s PPX’s (think Elixir Macro’s) that do that for you in but a single line. :slight_smile:

It shouldn’t really be that bad? Just something like external the_thing : int -> int -> void = "javascript_side_call". You need to define the types it takes so it knows when you have a bug. ^.^

I find it more sensible in OCaml personally, ReasonML syntax is this weird abomination between OCaml and Javascript and it’s just yeesh to me… >.>

For note, that same code you gave in OCaml would be more like:

external init : endPoint -> ?opts:'opts Js.t -> unit -> t = "Socket"[@@bs.module
                                                                    (("phoenix")
                                                                    [@reason.raw_literal
                                                                    "phoenix"])]
[@@bs.new ]
let init ?opts:(opts : 'opts Js.t option)  path = init path ?opts ()

There is the external followed by the name you want to call things, then the types you want to use (I am not a fan of Js.t types, I prefer to explicitly define what it should or should not be personally, far far better error checking as Js.t may as well just be a dynamic type and makes your code impossible to compile on native), then the javascript-side definition. The things in [...] are attributes, like @Blah in Java or so, and those are all bucklescript specific things, that is not normal OCaml usage (bucklescript is divering more and more from OCaml over time…). The it defines an init function, which just reshuffles arguments around and calls the external function by name with them, seems a bit verbose there too, it could have just been let init ?opts path = init path ?opts () or so and still be entirely fully typed.

3 Likes