Object-Oriented and Procedural Programming
Object-Oriented and Procedural Programming
Procedural programming involves analyzing the steps needed to solve a problem and then implementing those steps sequentially using functions. When using it, you simply call each function one by one.
Object-oriented programming breaks down the issues into various objects. The purpose of creating objects is not to complete a step but to describe the behavior of a particular entity within the entire problem-solving process.
Differences:
Procedural Programming: Emphasizes functional behavior, using functions as the smallest unit, focusing on how to do things.
Object-Oriented Programming: Encapsulates functionality within objects, emphasizing objects that have capabilities, using classes/objects as the smallest unit, focusing on who does the work.
Personally, I believe that objects are a secondary encapsulation of functions, allowing each object to have different processes/functions, greatly improving code readability and facilitating later maintenance and updates.
An Example
Example: A person puts an elephant in the refrigerator.
Procedural Analysis Process:
Step 1: Open the refrigerator door;
Step 2: Place the elephant inside the refrigerator;
Step 3: Close the refrigerator door.
Object-Oriented Analysis Process:
Step 1: Analyze which entities are involved in the actions;
// Person, Refrigerator, Elephant
Step 2: Define the subjects, adding attributes and functionalities to them;
// Person: The person needs to have the ability to open and close the refrigerator and put the elephant inside;
// Refrigerator: The refrigerator needs to have the ability to open and close its door;
// Elephant: The elephant needs to have the ability to enter the refrigerator.
Advantages and Disadvantages
Procedural Programming
Advantages:
The process-oriented approach clarifies programming tasks, considering implementation methods and final results before development, with clear specific steps that facilitate node analysis.
High efficiency: Procedural programming emphasizes concise and effective code, adeptly integrating data structures to develop high-efficiency programs.
Disadvantages:
- Requires deep thinking and is mentally taxing; low code reusability, poor extensibility, and more challenging maintenance in the later stages.
Object-Oriented Programming
Advantages:
Clear structure: Programs are modular and organized, aligning more closely with human thought processes.
Easy to extend: High code reusability; supports inheritance and overriding; can design low-coupling systems. Easy maintenance: The low coupling of systems reduces the workload for later program maintenance.
Disadvantages:
High overhead: When modifying the internal workings of an object, its attributes are not directly accessible from outside, necessitating the addition of many behaviors that serve no other purpose than to read or write. This increases the burden of programming, raises runtime overhead, and makes programs appear bloated.
Lower performance: Due to operating at a higher level of logical abstraction, object-oriented programming must sacrifice performance during implementation, resulting in significant computation time and memory storage overhead.