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.

→ More replies (4)