There are no methods in Elixir - just functions!

I’m glad José posted the below in another thread as I was starting to get a little confused after hearing them being referred to as methods:

Methods are found in Object Orientated Programming (Elixir is a functional programming language), here’s how they differ courtesy of this Stack Overflow question:

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  • A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).

(This is a simplified explanation, ignoring issues of scope etc.)

Hopefully this thread will help save someone else from falling into the trap of thinking of them as methods.

6 Likes

What’s the definition of “operating on data”? If it means changing data then I agree. However, if it means also reading data, then what about config vars and module attributes? Those are not explicitly passed onto Elixir functions and can be read.

1 Like

Frankly, when and if you have a chance - correct them. Postel’s law is great in software but when it comes to human communication sloppiness resulting from “you know what I mean” tolerance seems to be cumulative over time ultimately resulting in statements that completely obfuscate the intended meaning.

That being said it needs the be recognized that “method” really refers to a function playing a particular role. Many moons ago I wasn’t allowed to use C++ on a particular project - so I resorted to C Interfaces and Implementations (1996) tactics which allowed me to be quasi-OO with just functions and structs. This is exactly the trap that Dave Thomas warns about in Programming Elixir 1.3 p.91:

Structs in particular might lead you into the darkness because you can associate functions with them in their module definitions. At some point, the old object-orientation neurons still active in the nether regions of your brain might burst into life and you might think, “Hey, this is a bit like a class definition.” And you’d be right. You can write something akin to object-oriented code using structs (or maps) and modules.

This is a bad idea. Not because objects are intrinsically bad, but because you’ll be mixing paradigms and diluting the benefits a functional approach gives you
.
Stay pure, young coder. Stay pure.

So even in the absence of methods inside the language, functions can still be used in method-like manner (topic of this recent discussion) which can be an even more insidious problem.

3 Likes

I think the SO answer is general, and we could probably make some changes to make it more applicable to functional programming. Perhaps we could have a stab at re-writing it from Elixir’s perspective?

Functions vs Methods in Elixir

There are no methods in Elixir because methods are related to Object Orientated Programming. Here’s how they differ:

A function is a piece of code that can be called to run. It can be passed data to operate on (i.e. the parameters) and can optionally return data. In Elixir and other immutable languages, this data is a new copy of that data and not a modified version of what was passed into it. All data that is passed to a function is explicitly passed.

A method on the other hand is a piece of code that is called by a name that is associated with an object (which we do not have in Elixir). Methods differ from functions in two key ways:

  • A method is implicitly passed the object on which it was called.
  • A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).

Feel free to quote/edit/expand :003:

I agree @peerreynders - I really like that quote from Dave too, it’s a good reminder that OOP and Functional are different paradigms.

they are copied at compile time so it’s just a shorthand for defining it in place

3 Likes