I´m a beginner in programming, and I´ve been wondering now why classes are so much more useful than functions. I know classes are good, but I don´t see why they are better than just having multiple small functions in a bigger function.
I´m a beginner in programming, and I´ve been wondering now why classes are so much more useful than functions. I know classes are good, but I don´t see why they are better than just having multiple small functions in a bigger function.
Global variables... Encapsulation... OOP... etc.
There's a saying about this: "objects are a poor man's closures, and closures are a poor man's objects": https://stackoverflow.com/questions/2497801/closures-are-poor-mans-objects-and-vice-versa-what-does-this-mean
Classes also have data fields that goes along with those functions.
Because, before anything, a class is supposed to represent a state. You can't achieve that with functions within a function, once the function returns all variables are lost.
It is perfectly possible to code without classes, but that's just giving away a very useful tool.
you know, all the stuff m'kay
Re-usability, decoupling, and structure. What you're describing quickly leads to the infamous "spaghetti code".
Classes are better because they provide an easy way to keep track of object _state_, i.e. variables.
That would be harder to do if you just had a bunch of functions.
my surface take-away: either case, I'm a poor man
Objects (made with classes) let you sort data better. Imagine a list of people and data associated with them, without a class creating an object type you would have to use arrays.
Using classes as functions will slow down your code, but for some projects you don't really need that much speed. Classes are useful for some things and should be avoided for others. Classes are part of a complete breakfast.
Nah, a closure has state. It's just different ways of implementing the same idea.
But classes are a straightforward way of declaring that you want to look at your program data in terms of clumps of related state stored together. Usually each clump corresponds to some named entity in the business logic.
Well, you are able to have all of that with functions/functional approaches, too.
So, you use functions to package up functionality, usually a behaviour (side effects) or just a value that depends on inputs.
Classes you would generally think of them as a type, like something you can create. It will then carry its context (constructor params) everywhere it goes. You would add methods to that class so you can use that context again and again without passing them to that function. This specific use case is basically function currying or partial application of functions, but you are free to reuse that context in more than one way.
Still on seeing classes as types, you can then instead of passing 6 numbers that correspond to 2 points in 3d space and having to call them x1, y1, z1, x2, y2, z2; you can package them up in a point(x, y, z) and just pass point1, point2. (Yes this is basic usage of structs but many languages conflate the them anyway)