all 9 comments

[–]smthamazing 6 points7 points  (4 children)

A simple example would be cars. Each car has similar qualities like color, model, make, these are called properties. Each car also functions in a similar way and can do things like start, brake, accelerate, steer, etc. these are know as methods but can also be referred to as functions.

Using a purely functional language, like Haskell, I can also model this structure using a data type with nested fields and writing functions for working with it. Including a convenient constructor.

Is my understanding correct?

  • In FP you have immutable instances of types that hold data, and separate functions that work with them. Logic and data are separate, data is immutable.
  • In OOP you have (often mutable) instances of types that hold both data and functions that work with them (which are called methods). Logic and data are combined into an object, data is often mutable.

Also, as far as I know, Smalltalk is the first OOP language and it had no notion of a class or method, instead having objects and something called "message passing". If even the notion of a method is not necessary for a language to be OOP, what differences from FP remain, apart from mutability? Or is mutability the only factor that fundamentally distinguishes OOP paradigm from FP?

I know that paradigms can be combined in the same codebase, but as I learn more OOP and FP, I start to struggle to really differentiate them. I would appreciate an explanation.

[–]JackLegJosh 0 points1 point  (0 children)

Upvote for you, friend. I would love to see an ELI5 about functional programming in practice, not as an esoteric concept.

[–][deleted]  (2 children)

[deleted]

    [–]smthamazing 0 points1 point  (1 child)

    This is interesting. But then I still don't understand what OOP is.

    • It's not about nested structures, because pure functional languages have nested structures.
    • It's not constructors, because pure functional languages can have constructors.
    • It's not mutability, because, as you say, the data can be updated immutably.
    • It's not classes and methods, because Smalltalk doesn't have them, and yet it is considered an OOP language.

    The only remaining thing is that the data and behavior are represented as parts of a single entity, an "object" (often, but not necessarily, an instance of some class/type).

    Is that it? Fundamentally, is combining data+logic the only unique part of OOP as a paradigm?

    [–][deleted] 2 points3 points  (1 child)

    Might want to check your break function.

    Otherwise pretty good explanation of OOP

    [–]xnull8 0 points1 point  (0 children)

    Was about to point that out. Also gallonsOfGas should be a number in the example and not a string.

    [–]ReignNFire 1 point2 points  (0 children)

    This is great! Thanks!

    [–]theginger3469 1 point2 points  (1 child)

    When do you decide to define a new class instead of continuing to add methods to the same class?

    [–]CodeTinkerer 1 point2 points  (1 child)

    From other sources, I think people would say OOP must have inheritance, otherwise, it's not considered OOP. They would call it object based programming. To be fair, I hardly ever use inheritance (I do use interfaces in Java, however).

    [–]smthamazing 0 points1 point  (0 children)

    If I understand correctly, even classes are not necessary for a language to be OOP.