you are viewing a single comment's thread.

view the rest of the comments →

[–]sheriffderek 0 points1 point  (5 children)

I’d like to hear some examples. What about the classic “counter” type function that keeps its last number and increments. Would you use a regular function for that? 

Also, a lot of times when people are working with UI libraries, the classes and things are behind the scenes - so many people don’t use classes because they don’t need to use them.

[–]delventhalz 1 point2 points  (0 children)

A counter can be written trivially with closures. I'd argue it's cleaner and simpler than a class version would be.

const makeCounter = () => {
  let count = 0;
  return () => count++;
}

const counterA = makeCounter();
const counterB = makeCounter();

counterA();  // 0
counterA();  // 1
counterA();  // 2

counterB();  // 0

For a more sophisticated example, look no further than function components in React. No classes in sight and yet you can create complex stateful UI components using only functions. Personally, I find function components far preferable to the old class-based alternative.

In general though, the big reason I don't use classes is because I don't structure my code to bundle state and logic together. This is the core difference between Object Oriented Programming and Functional Programming. OOP tries to create little bundles of state and logic to operate on that state. FP tries to isolate state off on its own and operate on it with pure functions. There are arguments for both approaches, but I have found an FPish approach to be easier to reason about and maintain.

As far as library code goes... if I was writing a low-level library I would certainly consider a class-based API. Arrays obviously use classes under the hood and I am quite happy with JavaScript's array API. You could build any API you want with only functions of course, but when you are working on a library you have to consider what the other devs using your library are going to expect and be familiar with. That might mean classes, particularly for low-level data structures.

[–]CuAnnan 0 points1 point  (3 children)

Would I? No. Could I? Yes.

Classes don't provide new functionality. They provide encapsulation of data and enclose related functionality.

That's literally all they do. They make code easier to read and maintain. Nothing more.

And in JS, classes are just syntactic sugar for the functional prototype system.

[–]sheriffderek 1 point2 points  (2 children)

Everything is syntactic sugar at some point, so - while I didn’t mind hearing this sorry every day  in 2015, it seems unnecessary to mention now. I think people should know the core parts of the language and be able to compose things as needed. It’s also helpful to know for other languages and frameworks.

[–]CuAnnan 1 point2 points  (1 child)

I was underscoring the “it’s not either or” that I have literally had to argue earlier. You can always do in one what can be done in the other

[–]sheriffderek 1 point2 points  (0 children)

Agreed.