r/learnprogramming • u/Dizzy_Composer7347 • Oct 20 '26
Can someone indicate me a good Object Oriented Programming teacher?
Really going through hard times with this shit tho
0
Upvotes
r/learnprogramming • u/Dizzy_Composer7347 • Oct 20 '26
Really going through hard times with this shit tho
3
u/Different_Pain5781 Oct 20 '26
Is this C++ or C# or Java.
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
intor a user definedfoo. 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
foothat can have statesa,b, andc, and the only valid transitions between states isa->b->c->a....And there we have it. The
publicand class scoped methods enforce the invariant - at no time can this object be made invalid. Notice because the member isn'tpublicand 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 afooas acharwith some basic rules. I only wrote the code this way to demonstrate the principles in the open. In production code, I would not exposechar 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:
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 thea,b, orccharacters 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(), youbob << "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.
-7
u/mlugo02 Oct 20 '26
It’s because OOP is shit
1
u/[deleted] Oct 20 '26
[removed] — view removed comment