0

I was wondering if pure classes make sense or not (as a concept)?

For example the constraints would be:

  • No inheritance (only composition)

  • All dependencies are passed in the constructor, or methods

For example:

class Elem {
  constructor(el) {
    this.el = el;
  }
  get html() {
    return this.el.innerHTML;
  }
  get cls() {
    return this.el.className.split(' ');
  }
  set cls(arr) {
    this.el.className(arr.join(' '));
  }
  hide() {
    this.el.style.display = 'none';
  }
}

const appEl = document.getElementById('app');
const app = new Elem(appEl);
app.cls = ['flex', 'h100'];
app.hide();

Just wondering if having such constraints would add benefits similar to pure functions?

Update

I think the above example is bad, because this.el lives somewhere else I guess (in the dom), but what about:

class One {
  constructor() {
    this.val = 1;
  }
  add(val) {
    return this.val + val;
  }
}

const one = new One;
one.add(6);
Tariq Qubti
  • 43
  • 1
  • 6
  • Under the hood they will only be functions anyway, classes dont exist in JS, it's just syntactic sugar. But to answer you're question, yes, it could offer a layer of abstraction, and usually that's a good thing.. – andy mccullough Feb 12 '20 at 18:14
  • 1
    None of the constraints you mentioned have anything to do with pure functions? Also your example class is definitely not pure, as its `cls` setter and the `hide` method do side effects. – Bergi Feb 12 '20 at 19:22
  • I was wondering if you can think of constraints that would make classes have the same benefits of pure functions (easier testing, clearer, etc.) – Tariq Qubti Feb 13 '20 at 02:14

1 Answers1

2

Using functions doesn't make you a functional programmer. You need to understand what is it that functional programming tries to achieve.

For example a pure function:

  1. Does not mutate its argument(s)
  2. Returns the same output for the same input
  3. Has no side effects

So I hear that a class gets converted into function(s) under the hood... Fine. Will they be pure? It depends.

In your particular case, they're not. And that is because of the nature of the thing you're working with: a DOM element.

Your .html() method is almost guaranteed to be impure for example:

const el = document.querySelector('#foo');

const elem = new Elem(el);

elem.html();
//=> e.g. "foo"

el.innerHTML = "bar";

elem.html();
//=> "bar"

The outside world has the power to modify the behaviour of your functions. Hence your functions are impure and therefore your class and all its instances.

On the other hand, your class can also change the world:

elem.cls = ['become-super-big'];

This may or may not make your app unusable and/or conflicts with other parts of your app. That's definitely not a good thing.

What I personally try to achieve with functional programming (it's not easy believe me), is predictability. Your system and its smaller parts should all have a predictable behaviour under the same circumstance. Always, all the time. No compromise.

Can you have a pure class? Interesting question! Maybe. But certainly not in the way you're approaching it I'm afraid.

customcommander
  • 17,580
  • 5
  • 58
  • 84
  • Yes I understand it is not just about using functions, I think my example was bad – Tariq Qubti Feb 12 '20 at 19:12
  • @TariqQubti I think your question is very interesting nonetheless! – customcommander Feb 12 '20 at 19:21
  • What does "*Does not access variables outside of its defined scope*" mean? Surely you cannot access a variable if it's not in scope anyway. – Bergi Feb 12 '20 at 19:23
  • @customcommander It's not about a variable "not intended to be used". It's about the variable (or its value) being *mutable*. Accessing any variable, even global ones and window properties, is fine as long as they are *constant*. See also [here](https://stackoverflow.com/a/54992348/1048572) – Bergi Feb 12 '20 at 19:31
  • 1
    Also I think "*Does not mutate its argument(s)*" could be removed as it is implied by "#3 no side effects", but of course that assumes every reader knows what "side effect" means. – Bergi Feb 12 '20 at 19:32
  • @Bergi wrt to your last suggestion I'll leave it as is. You can produce side effects without mutating your arguments. But I do see what you mean. – customcommander Feb 12 '20 at 19:35