r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

15

u/rhett21 Apr 05 '22

OOP. Currently taking DSA in C++, always confused what's an object, and when does an object get promoted to OODesign.

16

u/lurgi Apr 05 '22

There's no real promotion.

An object in most languages is first a way of bundling up data and functions that operate on that data in a handy way. That's encapsulation. Usually you also have a way of saying "These things are a lot like these other things, but they have extra stuff, too". That's sub-classing. Finally, you typically have a way of hiding some details from the people who are using your class/object. The sorts of inner workings that they don't need to know about, but you, the author of the class, do need. That's information hiding.

It's not exactly clear why this is so useful when you are working on small programs. Then again, just about everything works with small programs. You can use goto all over the place but everything is small enough that you can grasp the whole thing. A lot of programming techniques are about managing complexity as programs grow.

Anyway, what is a class? Typically, classes are nouns. They are things. These things "do stuff".

Imagine you are making a Connect 4 game. What are the "things" in the game? Well, there's a Board. That's a start. The board contains a bunch of Pieces (these could be classes, but probably aren't complex enough to merit it). What sort of things does the Board do? How about playing a piece? We could just put the piece at the specific location, but it's better if we tell the Board "Player 1 is dropping a piece on column 2" and let the Board work out where it goes from there. We can also ask the Board "Did anyone win?".

The nice part is that all the game logic is bundled up in the Board class rather than being scattered everywhere.

3

u/TangerineX Apr 05 '22

I recommend reading Design Patterns: Elements of Reusable Object-Oriented Software (you can find PDFs online). While objects and services and classes make up the basis of OOP, there are larger meta-structures that help you understand how to actually write code in an OOP environment. Design Patterns are sort of like bigger patterns of code that a lot of other engineers have gone through and realized are applicable very often. Learning about design patterns really helps understand how OOP works, and how to write better OOP.

For each design pattern, it's important to understand the following

  1. What problems it seeks to solve/when to use the design pattern

  2. Why is the design pattern helpful? How does it work?

  3. How to implement the design pattern.

0

u/GreenScarz Apr 06 '22 edited Apr 06 '22

One of the best definitions of the concept (IMO) was written by CAR Hoare,

“A procedure which is capable of giving rise to block instances which survive its call will be known as a class; and the instances will be known as objects of that class.”

This is a very fundamental definition of an object.

Consider the following:

function stack() {
    let data = []
    return {
        push: (item) => data.unshift(item),
        pop: () => data.shift()
    }
}

There’s no class keyword, but this is a class none the less, and calling stack yields an instance of this class. The “block” here is everything within the scope of the function call. But, notice that we’re returning a set of abstractions that govern how you can use this data array, even after the function exits. A “stack” in this case is something to which you can push and pop.

Fundamentally, OOP is a means of reducing complexity by abstracting state.

1

u/TheTomato2 Apr 05 '22

Oh man, OOP doesn't really have a real definition. OOP is programing fad that started in the 90s and is now kind of subsiding now a bit. For some people its a holy war. But the gist is that just a way to of thinking about programming in "objects" that don't actually exist inside the computer. It's a like a vague way to conceptualize a program. So asking "what is an object" is like asking what is a thing"? Some very OOP languages the "Object" is literally built into the language (C#, Java), other language its bolted on Frankenstein style(C++). I am not to get into all the OOP specifics, but its more of a way to approach thinking about programing than any specific actual concrete property. Sometimes it's a good way, but a lot of the times its really not. Its has no consideration of what the hardware is really doing, which is what Data Oriented Design does, and it often time creates some overly complex spaghetti that had no business being there.

The real issue with OOP is that it seems seductively simple when it really isn't, and that its taught as the way to program rather than a way to program among many. And for C++ you shouldn't be taking an OOP approach unless the problem demands it, its OOP is halfhearlty implemented in the language, like half of C++ features. Encapsulation and API boundaries aren't unique to OOP. For DSA I would personally take a more functional programming style approach. If you have any specific questions I can answer them as I mostly use C++, just understand objects aren't like a concrete thing in C++, just a (usually misguided) way to mentally model how a program works.

1

u/[deleted] Apr 06 '22

OOP is a paradigm that attempts to communicate in code the concepts that exist in an application (e.g. Order, etc.). There should be a mapping (ideally anyway) between the real world and its representation in software. That's it really.

To understand how to apply it for business concerns, IMO, you need some background in business analysis - specifically how business entities are defined and how business processes are modeled. Business analysis is not a software development discipline, it's different altogether, you can do business analysis to improve the processes in a company without involving software. But it is useful to know of its modeling tools, because then you'd see how business objects can be represented as classes, or how certain business processes can be represented as classes.

Of course there's a different way of applying OOP for technical concerns (e.g. creating a database adapter, a wrapper for a REST API). Although most of the techniques in this area is laid out in common design patterns.

They key to applying OOP effectively is to make sure you are distinguishing from business concerns and technical concerns. For business concerns, never force a design pattern to accomplish them, use plain objects and try to mirror the abstract model (e.g., DFD) for your business concerns when you can. While technical concerns are rigorously structured (e.g., rules for a database adapter), the real world from which business concerns derive from is chaotic and constantly change, so don't over-engineer with patterns on this end, use plain objects as much as possible.

1

u/Sexual_tomato Apr 06 '22

I didn't get the point of OOP until I started writing larger programs.

At it's most basic level, it groups data with the functions that operate on them to define it's behavior.

Say you wanted to start doing stuff with 2d vectors. If you defined a bunch of functions that took in x and y coordinates to do stuff like add them together, compare them, get magnitude, change coordinate systems, etc, the logic would get really cumbersome to use really fast.

Instead, if you defined a 2d vector class and wrote the logic for operating on points in the class itself, stuff like the following:

def vector2d_equals(x1, y1, x2, y2):
    return x1==x2 and y1==y2

x1=1
x2=2
y1=1
y2=2

print(vector2d_equals(x1, y1, x2, y2))

>>> False

Goes to

class Vector2d:
    def __init__(self, x, y)
        self.x = x
        self.y = y
    
    def __eq__(self, other):
        return self.x == other.x and self.y==other.y

v1 = Vector2d(1, 1)
v2 = Vector2d(2, 2)

print(v1 == v2)
>>> False

While the definition is a bit more complex, at point of use it's far simpler to reason about.

For me, it lets me define how the small parts of my program should work. Then, I can work with well defined concepts to build bigger things on the primitives I've built. Eventually building up to the highest levels where the main function of my application is only ~100 lines or so.