Sign in to follow this  
rippingcorpse

[C++] Programming paradigms

Recommended Posts

When is it best to use object-oriented programming and when is it best to use procedural programming? I'm understanding most of what I'm learning but I'm having a little bit of trouble understanding how I should organize and structure my code. Thanks in advance. If I'm asking too many questions, let me know.

Share this post


Link to post
Share on other sites
Think of it this way. Using the object-oriented approach, you basically send messages to objects. For instance, Cube.Draw() sends a "Draw" message to the object "Cube", which responds by drawing itself.

On the contrary, the procedural approach means that you have functions that operate on "objects". For example, Collide(Object1,Object2) is a function that operates on "Object1" and "Object2".

These two can be mixed. For instance:

void Collide(obj1,obj2)
{
//do some stuff...
obj1->Explode();
obj2->Explode();
//do some other stuff...
}

Basically, sometimes it makes more sense to put a piece of code inside a method, and others inside a free function. The above example could be refactored to be entirely OO-for instance you could have a "Collider" object that receives the message "DoCollision" with obj1,obj2 as parameters. It basically boils down to how the programmer sees things and what are his needs. There are no strict rules here. The important thing is to have a clean, modular design(you might-no, you do- want to google and read about the 5 principles of Object Oriented Design), not putting everything inside a class even when you don't need to.

And lastly, OO is not a panacea. You can have good procedural code, and bad OO code. There are also other paradigms you could learn, such as functional, relational,aspect-oriented... Each paradigm is good in solving a domain of problems. In big programs, the objective is not to pick a paradigm and stick to it throughout the project no matter if it makes sense or not. It's to solve each problem as elegantly and efficiently as possible. In fact, my opinion is that the most elegant programs are the ones that use several paradigms, according to what the current needs are.

Share this post


Link to post
Share on other sites
Quote:
When is it best to use object-oriented programming and when is it best to use procedural programming?
There isn’t any rule book on what programming style to use. As a note, procedural and object-oriented styles of programming do not oppose each other. The best way I have to describe my approach my decision making is to come up with the design independently, and then see what programming constructs I need to fulfill that design.

Quote:
I’m understanding most of what I’m learning but I’m having a little bit of trouble understanding how I should organize and structure my code.
Yes, it’s one thing to learn the syntax of a language, but it’s entirely another to see how they would be used in your code design. One’s ability to design their code properly comes from experience. No rulebook or guidelines, you need experience in writing real programs. After writing several programs, you start to see recurring design issues, and begin to understand what type of answers work and what are dead ends.

It’s frustrating to not grasp the big picture early on, but you will eventually gain the necessary insight. For now, just keep writing code, make mistakes or good choices, and reflect upon them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this