I have a few been studying OOP for some time. I keep reading in almost every book that OOP was developed in order to write more complex software and has many features not available in procedural programming like code re usability, security, modularity, encapsulation etc.

I haven't developed any real-world app yet so i don't really understand a few things and i hope some of you can explain them to me.

1. How is the code re-usable in OOP ?
2. How is OOP more secure than Procedural programming?

3. Compared to procedural programming how is OOP based programs more easily maintainable ? Both procedural and OOP have functions/methods so how aren't they both easily maintainable ?

1. How is the code re-usable in OOP ?

Click me.

2. How is OOP more secure than Procedural programming?

OOP is neither less nor more secure than procedural programming. Anyone who told you otherwise was trying to sell OOP by bullshitting.

3. Compared to procedural programming how is OOP based programs more easily maintainable ?

It's easier to structure object based applications to be less brittle in the face of changes. While it's not impossible to write an easily maintainable procedural application, it's quite a bit harder to visualize the structure enough to avoid tight coupling between unrelated components. The more your changes cause ripples throughout the code base, the harder it is to maintain a large system.

Fundamentally, a running application is made of data and processes to manipulate that data. A program to solve a problem is made of sub-programs to solve the sub-problems. OOP is just merging those two realities by bundling data and the related functions in one entity (an object) that serves to solve a sub-problem using _only_ the data relevant to this sub-problem. That is the one thing to understand correctly, the rest (encapsulation, abstraction, polymorphism, and various idioms) is accessory.

1. How is the code re-usable in OOP ?
It very often happens that sub-problems come back again and again in application development. If classes are programmed well, methods use only the data members of the object used to solve the sub-problem. Code is re-usable because if the sub-problem is properly abstracted in the code, the code can be reused to solve the same sub-problem in different application contexts. Of course, bad OOP is not re-usable, and good procedural programming is just as modular. But one could argue that good procedural programming is often just a basic form of OOP and no longer (pure) procedural programming. Remember that OOP is a paradigm that is only facilitated by C++ or Java, but can be done in (any) language like C or fortran. Finally, you will find that most aspects of OOP that are usually emphasised, like encapsulation, abstraction, polymorphism, and a plethora of idioms, are usually all aimed, in one way or another, at promoting independence between classes and objects of a software architecture, which is key to re-usability.

2. How is OOP more secure than Procedural programming?
I wouldn't go as far as Narue and say it is BS to claim OOP is more secure. I might be able to think of a few instances where procedural code is weaker on the security side. Of course, it depends on your definition of "security". If you are talking about internet security, or anti-viral qualities, and such, then, of course, OOP is no different than procedural programming because these issues are either at the algorithmic level (encryption) or at the networking level (either between computers or between programs), and OOP does really play a role in that (not that I know of). But I think "security" is usually meant in the sense of robustness of the applications, i.e. if it is prone to bugs and crashes. For that, I can certainly think of many reasons why OOP can tend to be more safe, especially with respect to memory management. But again, a good procedural programmer will produce very secure code as well (but such good programmers, in either OOP or procedural prog., are the exception, not the rule, and that is the assumption that is usually made when assessing the safety of a programming paradigm). Finally, it is also typical that people (maybe you, or the authors of the books you are reading) to confuse OOP vs. procedural prog. with C++ vs. C (as the two main representative languages of either paradigms). In this respect, C++ is much safer than C, but mainly because C++ is more strongly typed (it disallows or complains strongly about dangerous type conversions). Other procedural programming languages (like fortran) are safer than C, just because of stronger type policies.

3. Compared to procedural programming how is OOP based programs more easily maintainable ? Both procedural and OOP have functions/methods so how aren't they both easily maintainable ?
The key, again, is _independence_. Because OOP naturally introduces (C++), or enforces (Java), the regrouping of methods and related data together in a class, and the use of several aforementioned OOP techniques to promote independence of the classes and objects, it is easier for the programmer to make the extra step to ensure that small changes to the underlying implementation or data structure does not have a ripple effect over the entire application (or worse, all the users of the library). You can do the same in procedural programming and have very easily maintainable code, but achieving this is much harder without the clean and structured style of the code that OOP produces.

This article has been dead for over six months. Start a new discussion instead.