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

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews