+ 2
What is the difference between procedural and object oriented programming?
I have been trying to understand the difference between these two types of programming paradigms but i haven't been able to comoletely grasp the difference between them. it would.be most appreciated if someone could explain the difference to me inclusive of examples that explain the difference.
1 ответ
+ 13
Let's say you have a dream, you want to make a replica of Fruit Ninja, to make the dream real you have to code a lot and make your own sprites, maybe in Blender.
But let's focus on the code, in Fruit Ninja you have different type of fruit, you have bananas and apples, oranges and melons.
So, what you are going to do, is to create something like a variable for every single fruit in the game, the pseudo-code looks like:
var apple = {
color: red,
points: 30,
max_Combo: 3
}
var banana = {
color: yellow,
points: 50,
max_Combo: 1
}
var kiwi = {
color: green,
points: 20,
max_Combo: 8
}
... then, you are going to code line by line any function that helps you to build the logic for your game. It works... but it can be improved.
Let's try to write the same code but in a less procedural-way, the pseudo-code looks like:
class Fruit {
constructor(clr, pnt, combo) {
this.color = clr;
this.points = pnt;
this.max_Combo = combo;
}
}
... now what? Now you declared a class, and in the class we have a constructor that will be called automatically when you create an object, all you have to do now is to instantiate the Fruit class by creating an object... it seems hard, isn't it? Take a look at the pseudo-code:
var apple = new Fruit('red', 30, 3);
var banana = new Fruit('yellow', 50, 1);
var kiwi = new Fruit('green', 20, 8);
... that's it, the parameters inside the parentheses correspond to the parameters specified inside the constructor method.
Object Oriented Programming is much more than this, but it simple words... it helps you to SEPARATE the logic of your application in small pieces that you can reuse when you need, without have to rewrite everything from scratch.