By using Object Oriented Programming, you will have objects, that have asociated functions, that are (should) be the only way to modify its properties (internal variables).
It was common to have functions called trim_string(string)
, while with a string
class you could do string.trim()
. The difference is noticeable mainly when doing big complex modules, where you need to do all you can to minify the coupling between individual components.
There are other concepts that encompass OOP, like inheritance, but the real important thing to know, is that OOP is about making you think about objects that have operations and message passing (methods/verbs), instead of thinking in term of operations (functions/verbs) and basic elements (variables)
The importance of the object oriented paradigm is not as much in the language mechanism as it is in the thinking and design process.
Alto take a look at this question.
There is nothing inherently wrong about Structured Programming, its just that some problems map better to an Object Oriented design.
For example you could have in a SP language:
#Pseudocode!!!
function talk(dog):
if dog is aDog:
print "bark!"
raise "IS NOT A SUPPORTED ANIMAL!!!"
>>var dog as aDog
>>talk(dog)
"bark!"
>>var cat as aCat
>>talk(cat)
EXCEPTION: IS NOT A SUPPORTED ANIMAL!!!
# Lets add the cat
function talk(animal):
if animal is aDog:
print "bark!"
if animal is aCat:
print "miau!"
raise "IS NOT A SUPPORTED ANIMAL!!!"
While on an OOP youd have:
class Animal:
def __init__(self, name="skippy"):
self.name = name
def talk(self):
raise "MUTE ANIMAL"
class Dog(Animal):
def talk(self):
print "bark!"
class Cat(Animal):
def talk(self):
print "miau!"
>>dog = new Dog()
>>dog.talk()
"bark!"
>>cat = new Cat()
>>cat.talk()
"miau!"
You can see that with SP, every animal that you add, you'd have to add another if
to talk
, add another variable to store the name of the animal, touch potentially every function in the module, while on OOP, you can consider your class as independent to the rest. When there is a global change, you change the Animal
, when its a narrow change, you just have to look at the class definition.
For simple, secuencial, an possible throwaway code, its ok to use structured programming.