DEV Community

loading...

Discussion on: You don't need classes

Collapse
jcubic profile image
Jakub T. Jankiewicz

I agree that functional programming is create you make code composible with functions. But this is not the way you should write code in JavaScript. If you came from Haskell you will think that everything should be pure function and if you came from Java you think that everything should be a class. I've seen examples on twitter then someone wrote single class in JS just to define a function without any state, it was pure function.

Writing everything with functions or everything with classes is bad idea. Also your example of Clean is not every easy to understand, so I prefer classes for objects and using functions when I can benefit of them, not using function everywhere just because I want to have everything in functional style.

Thread Thread
lukeshiru profile image
△ LUKE知る Author

I understand your point, but the thing is: When do you really need classes? In which scenarios are classes more useful/clear than functions?

I mean I know that people say "use composition when you have a has-a relationship, and inheritance when you have an is-a relationship", but one could argue that when something has an is-a relationship, also has a has-a relationship, so the inheritance is not even needed.

Taking the same example in the post: Circle is-a Shape, so we use inheritance, right? Not quite. We could read that as Circle has a position and a name, so we don't need inheritance at all. This same principle can be applied to pretty much any class based architecture.

The main point of this article is that you can achieve the same stuff you can do with classes, without them. So if we forget about them we have a cleaner code, and one less thing to worry about.

The next posts in this series will be about how we can forget about other stuff, like flow control and even variables :D

Thread Thread
jcubic profile image
Jakub T. Jankiewicz • Edited

Yes you also don't need named functions and numbers, you can write everything with just anonymous functions (like lambda calculus). Check this article: Programming with Nothing, here is the code in JavaScript.

The reason why the code is simple is not because you use less of language features, but because you use features that are known by other person that read the code that also know that language.

This is not good way to go, your code will be less and less understandable by other programmers, but I will probably never convince you.

Thread Thread
lukeshiru profile image
△ LUKE知る Author

But you the idea with a good discussion is not to convince the other person, is to share knowledge so both parts end up more knowledgeable. If you provide information based on logic, and that logic is good enough, my point of view could shift in your direction.

You now added that "that is not good because is harder for other programmers", and I agree... but if you really think about it, is hard because they need to "unlearn" stuff that was taught to them as indispensable, and if they use less features of the language to achieve the same thing, they are basically using just english to express themselves.

You also mentioned "lambda calculus" and not needing named functions, and I don't agree with that. You need to have good names for your functions, constants and attributes, because you're using those to express the intent of your code, and without them then the code is unreadable. Haskell is a classic FP language, and I despise how they have super short names for functions, and 1 letter names for attributes. Is like they really don't want people to understand:

foldr :: (a -> b -> b) -> b -> [a] -> b
Enter fullscreen mode Exit fullscreen mode

vs

type reduceRight = <Item, Output = readonly Item[]>(
    reducer: Reducer<Item, Output>
) => (initialValue: Output) => (array: readonly Item[]) => Output;
Enter fullscreen mode Exit fullscreen mode

A Haskell fanboy would be like: "but .... but ... you wrote more there!" ... and that's true, but I did that once, and then when I use that function I have great documentation in the IDE itself while using it, instead of getting a, b, and so on.

TL;DR: The idea with this posts and the ones that will follow is not to suggest we should move from a programmer model to a mathematical one as Haskell folks would suggest, but to move from a programmer exclusive model, to one that is closer to plain english (easier to learn, easier to teach and easier to think about).

Some comments have been hidden by the post's author - find out more