AstonJ

AstonJ

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.

Showing Posts 1 to 5

sashaafm

sashaafm

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.

peerreynders

peerreynders

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.

AstonJ

AstonJ OP

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:

AstonJ

AstonJ OP

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

andre1sk

andre1sk

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

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
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
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
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
JesseHerrick
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews