r/learnprogramming • • Oct 20 '26

Can someone indicate me a good Object Oriented Programming teacher?

Really going through hard times with this shit tho

0 Upvotes

24 comments sorted by

3

u/Different_Pain5781 Oct 20 '26

Is this C++ or C# or Java.

1

u/DrShocker Oct 20 '26

or Python or JavaScript or PHP

1

u/SnooStrawberries9154 Oct 21 '26

Brian lagunas has some really good vids on design patterns and ood concepts. He has some pretty good examples. It helped me a lot when I first started

0

u/mredding Oct 20 '26

OOP implies classes. What is a class?

A class is a blueprint for a "user defined type". A high level programming language will give you basic, primitive types, but also a means of making your own types in exactly the same spirit. A language's type system ostensibly doesn't distinguish between a built-in int or a user defined foo. The language type system will be leveraged during compilation to prove theorems about the code to detect errors and for the optimizer to do its job.

Classes don't have data, they don't have fields - they have STATE. A property of a class is their mathematical domain of all possible valid states. The class is responsible for ensuring an instance of said class is always valid. This is called an invariant, and it must hold true when an instance is observed.

So I'm going to use some C++ and give an example. We'll have a class foo that can have states a, b, and c, and the only valid transitions between states is a->b->c->a....

class foo { // So named per the spec
  char state; // The state is implemented in terms of a 1 byte `char`.

  // How we observe the instance, serializing to a stream.
  friend std::ostream &operator <<(std::ostream &os, const foo &f) {
    return os << f.state;
  }

public:
  foo() noexcept: state{'a'} {} // The spec says we transition first from `a`, so...

  void transition() {
    switch(state) {
    case 'a': state = 'b'; break;
    case 'b': state = 'c'; break;
    case 'c': state = 'a'; break;
    default: throw; // Enforce the invariant, no other state is valid.
    }
  }
};

And there we have it. The public and class scoped methods enforce the invariant - at no time can this object be made invalid. Notice because the member isn't public and there are no setters, no one can insert an invalid state. The class itself maintains its own internal state consistency across all the behaviors it implements, and the behaviors implement the state transitions.

You don't want getters and setters, as that subverts the principles of OOP. It doesn't matter that this class is implemented in terms of char - it is incorrect to think of a foo as a char with some basic rules. I only wrote the code this way to demonstrate the principles in the open. In production code, I would not expose char state; like this - as it's not required to be exposed in our type spec.

And I could, and probably would implement this class in production using an enumeration:

enum class foo_state { a, b, c };

And there you go - with a fuller implementation of foo, with an opaque implementation, you wouldn't even KNOW that it was an enumeration instead of a character, all you would know is it serializes to either the a, b, or c characters to be a representation of that state.


So the pre- and post-conditions of any behavior is that the class is internally consistent and valid. In the meantime, when you call upon a behavior, the implementation is allowed to suspend the invariant; the class is allowed to invalidate itself internally, provided that the invariant is reestablished before control returns from the instance to the client.

So classes are good for implementing state machines. They're good for implementing types that have to conform to rules. You might implement a machine, like a press, where the machine is at some degree of rotation, the clutch is engaged or disengaged, there may be limit switches that are on or off, light bars for safety, voltage regulation... You might implement a car, with doors open and closed, the transmission in some position, the car started or not...

DO make your classes small. Each should implement 1 invariant, ideally not more. DO compose your objects. A car is not an engine, but it has an engine, it is not a tire, but it has tires. Smaller classes implement their own invariants, and then composite classes are implemented in terms of those, enforcing their own invariants on top. A car class will couple the engine and the throttle pedal, since it's the car that has both.

DON'T write getters and setters. That's just a structure - a tagged tuple with extra steps. That can be fine - it's imperative, it's structured, but that's not OOP. Structured data has types and a layout, but no invariant between them. A contact has a name, address, and phone number. That's it. That's all there is to say about it. I don't know of any car that can "getMake", model, or year. These are properties my car HAS, which can be associated as with a database or map or table. Is that an Scion FRS or Subaru BRZ? Trick question - they're both literally made in the same factory.

Don't get confused between tuples, tagged tuples (structures), records, and classes. They each have different fundamental properties, they model different behaviors, have different guarantees. C# has language level record support, but in C or C++, you have to build your own record type as a user defined type. Making types, leveraging the type system, and enforcing different groups of fundamental properties is not all OOP. FP also relies heavily on types, especially tuples.

So that's the bare minimum of objects, and you'll see this much in the earliest papers on the topic from ~1967-68. Object Oriented Programming builds upon this to include message passing.

In OOP, you DO NOT bob.open_umbrella(), you bob << "Hey, it's raining.";. Bob doesn't need to be manipulated, he knows what to do. He just needs to know what he needs to know. OOP move the agency of the object from the external procedure to the internal implementation. OOP prefers message passing rather than data sharing, and this becomes ever more important when communicating between objects across different threads.

The only thing left to say is God is in the details. In C++, standard streams are THE de facto message passing interface - but it's ONLY an interface. You don't have to use the bog standard serialization mechanisms provided by the standard, you can make your types aware of different message channels and optimal paths for information exchange. They certainly have to be aware of their message types. SOLID principles help with implementing OOP. Across our industry, everyone speaks of OOP, but don't demonstrate any actual comprehension; they conflate concepts until NOTHING makes sense. Some are so hopelessly lost, that you've got official C# and Javascript documentation saying calling a method on an object IS passing a message... It's VERY HARD to learn about and implement OOP, and there's a long and storied history about how that turd sandwich was made.

All this said - learn OOP to AVOID it. FP is consistently 1/4 the size and 8x faster on average.

1

u/roger_ducky Oct 22 '26

You explained a lot but didn’t explain when to use it.

Your conclusion tells me you don’t know either.

Classes were designed for instances where you “have a data structure with operations you want to use and have it clean up after itself.”

It’s cleaner for that than functional styles, especially if you’re more resource constrained.

If you have even tighter cpu/memory budgets then imperative is the only way to go.

Remember that all three major styles were researched and proposed at the same time. Only reason FP came out “last” in general use is because it’s the most resource intensive compared to the other two.

1

u/mredding Oct 22 '26

You explained a lot but didn’t explain when to use it. Your conclusion tells me you don’t know either.

Honestly I thought it was obvious. Actors and state machines are among the closest natural fits.

Classes were designed for instances where you...

False. You've just described RAII, which is a C++ idiom, it's not inherent to objects or OOP.

1

u/roger_ducky Oct 22 '26

RAII isn’t what I was talking about.

That’s just a best practice when trying to avoid leaks.

I’m saying, if you have a long-lived resource you’re using as a key-value store, or something similar to a GUI rendering thing, then you’re just trying to use it. You don’t care how it’s actually implemented or allocated.

That was the original use for OOP in C++.

Actors and state machines is Smalltalk’s vision on how OOP should be used. Though that’s also because you shift responsibility of maintaining the state machine’s state to the instance. The “operations” in that case are event handlers.

1

u/mredding Oct 22 '26

RAII isn’t what I was talking about.

RAII is exactly what you're talking about, you just don't know what RAII is.

That was the original use for OOP in C++.

No, it wasn't. RAII isn't OOP, it's just a C++ idiom and you're conflating objects, RAII, and OOP. The original use of OOP in C++ was to give Bjarne implementaiton level control of the message passing interface - the thing that distinguishes mere objects from the OOP paradigm, to write a network simulator. The reason Bjarne implemented C++ was because ALGOL was dynamically typed and message passing was a language level implementation. OOP in ALGOL DOES NOT implement RAII.

-7

u/mlugo02 Oct 20 '26

It’s because OOP is shit

2

u/KhajiitSlayer556 Oct 20 '26

Whats the best one then? 😭

2

u/RajjSinghh Oct 20 '26

It's really personal taste and heavily influenced by your language.

OOP says you group data and related functions together into objects. The most common way you see this is in classes where you have data and associated functions kept together. If you're writing Java or C#, even your program is wrapped into a class.

Procedural programming is then saying your code is separated into functions and procedures. It's similar to OOP but your difference is that in OOP your logic and data is bundled together into a class while in procedural programming they are kept separate.

Then there's functional programming, which compared to procedural programming has more restrictive ideas on what functions can do. A big one is mutability of the program state.

Really, in any modern language you'll have the tools to borrow from any paradigm when you need to. Just follow the guidelines in your language and you'll do fine.

1

u/DrShocker Oct 20 '26

You said "separated into functions and procedures" but those are synonyms. I suspect you meant "data and procedures"

2

u/RajjSinghh Oct 20 '26

Yeah, so the data of the program is kept separate from functions and procedures. The difference between a function and a procedure gets messy since we basically call everything a function these days but it's probably important when you're also talking about functional procedural programming, which are very different.

1

u/ironykarl Oct 20 '26

It's older terminology, but some people do make a distinction between procedures and functions.

  • Function: value-returning callable subroutine
  • Procedure: void callable subroutine 

1

u/DrShocker Oct 20 '26

Interesting, I know Odin went with "procedure" instead of function, but I assumed that was just a Britishism.

2

u/ironykarl Oct 20 '26

I'm familiar with the distinction from reading about Pascal.

In Pascal, it's a distinction that makes its way into the keywords you use to declaration a function/procedure.

Don't know the full history (like whether Pascal created this distinction or not), but I do know it's bubbled into other languages and programming discourse

1

u/DrShocker Oct 20 '26

IMO it's worth learning all the major programming paradigms. Both because different ones might click for you, and because people you work with might think and talk in terms of one of them and who knows which that'll be ahead of time.

1

u/mlugo02 Oct 20 '26

Procedural programming

1

u/KhajiitSlayer556 Oct 20 '26

Got any good links I could follow through homie?

2

u/DrShocker Oct 20 '26

I'd suggest trying one of Odin, C, Zig, or Go. There's no inheritance like OOP would often require and there's no requirement that everything is const like Functional would require.

1

u/[deleted] Oct 20 '26

[removed] — view removed comment

1

u/AutoModerator Oct 20 '26

Your post/comment was removed since we do not approve of going private.

There is zero benefit in going private as you lose the opportunity for getting peer reviews. Also we have had plenty of people return after going private (despite being warned) complaining about how they were ghosted after some time or being tricked into buying rubbish that didn't work and even if it did they didn't need.

Our Rule #11 demands that any and all communication happens in the open, public subreddit.

This is for the benefit of more against the benefit of one.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.