Why procedural programming is better




















If you use pointers you are continuously jumping in the memory and your CPU need to reload the cache every time. OOP code is full of pointers: every object is stored by its memory address. You call new everywhere which spread your objects all over the memory making the cache optimization almost impossible unless you have an allocator or a garbage collector that keeps things close to each other. You call callbacks and virtual functions. The compiler usually can't inline the virtual functions and a virtual function call is relatively slow jump to the VMT, get the address of the virtual function, call it [this involves pushing the parameters and local variables on the stack, executing the function then popping everything].

This matters a lot when you have a loop running from 0 to 25 times in every second. By using procedural style there aren't virtual function and the optimizar can inline everything in those hot loops. If the project is so small that it would be contained within one class and is not going to be used for very long, I would consider using functions. Alternatively if the language you are using does not support OO e. I think the suitability of OOP depends more on the subject area you're working in than the size of the project.

There are some subject areas CAD, simulation modeling, etc. However, there are a lot of other domains where the mapping ends up being clumsy and incongruous. Many people using OOP for everything seem to spend a lot of time trying to pound square pegs into round holes.

OOP has it's place, but so do procedural programming, functional programming, etc. Look at the problem you're trying to solve, then choose a programming paradigm that allows you to write the simplest possible program to solve it.

Procedural programs can be simpler for a certain type of program. Typically, these are the short script-like programs. The two concepts are not mutually exclusive, it is very likely that you will use PP in conjunction with OOP, I can't see how to segregate them.

Consider this scenario: Your code is not OO. You have data structures and many functions throughout your progam that operate on the data structures. IF all is working and not going to be changed, who cares if it's OO or not? It's working. It's done. If you can get to that point faster writing procedurally, then maybe that's the way to go.

But are you sure it's not going to be changed? Let's say you're likely to add new types of data structures. Each time you add a new data structure type that you want those functions to operate on, you have to make sure you find and modify every one of those functions to add a new "else if" case to check for and add the behavior you want to affect the new type of data structure. The pain of this increases as the program gets larger and more complicated.

The more likely this is, the better off you would be going with the OO approach. And - are you sure that it's working with no bugs? More involved switching logic creates more complexity in testing each unit of code. With polymorphic method calls, the language handles the switching logic for you and each method can be simpler and more straightforward to test. However, I'd always go the OO-way. Even for lines. It's a superior approach in a long term, and the overhead is just an overrated excuse.

All the big things start small. One of the goals of OOP was to make reusability easier however it is not the only purpose. The key to learning to use objects effectively is Design Patterns. We are all used to the idea of algorithms which tell us how to combine different procedures and data structures to perform common tasks. Conversely look at Design Patterns by the Gang of Four for ideas on how to combine objects to perform common tasks.

Before I learned about Design Patterns I was pretty much in the dark about how to use objects effectively other than as a super type structure. Remember that implementing Interfaces is just as important if not more important than inheritance.

Since then Java, C , and other languages have moved interface up to more a focus. What interfaces are great for is precisely defining how two object interact with each. It is not about reusing behavior.

As it turns out much of our software is about how the different parts interact. So using interface gives a lot more productivity gain than trying to make reusable components.

Remember that like many other programming ideas Objects are a tool. You will have to use your best judgment as to how well they work for your project.

Instead they are exposed from library and used by the object that need them. Then there is are some math function that were made object oriented as their structure naturally lead to this setup. Taking a list of points and transforming it in on of several different types of cutting paths. Again use your best judgment. Part of your answer depends on what language you're using.

I know that in Python, it's pretty simple to move procedural code into a class, or a more formal object. One of my heuristics is a based on how the "state" of the situation is. If the procedure pollutes the namespace, or could possibly affect the global state in a bad, or unpredictable way , then encapsulating that function in an object or class is probably wise. The more Procedural code become good the closer it's to Functional.

And advantages of FP are well known. I always begin designing in a top-down fashion and in the top parts it's much easier to think in OOP terms.

But when comes the time to code some little specific parts you are much more productive with just procedure programming. OOP is cool in designing and in shaping the project, so that the divide-et-impera paradigm can be applied. But you cannot apply it in every aspect of your code, as it were a religion :. If you "think OO" when you're programming, then I'm not sure it makes sense to ask "when should I revert to procedural programming?

NET languages. If you have to make an effort to get past thinking procedurally, then I'd advise asking about how you can overcome that if you care to ; otherwise stay with procedural. If it's that much effort to get into OOP-mode, your OOP code probably won't work very well anyway until you get further along the learning curve. The bigger the project, the more OOP you should employ. You can write bad software in both concepts. Still, complex software are much easier to write, understand and maintain in OO languages than in procedural.

It was and still is a breath of fresh air. To this point, the arguments of using OO for DRY and encapsulation is just adding unnecessary complexity in terms of how implicit it is and just sheer of how many layers that a class can inherit a lot of properties and methods into it.

In my opinion, final class should be the default. Most studies have found that OO code is more concise than procedural code. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Programming Tutorials. Related Tutorials Data Science. Intro to Programming.

Recommended Learning Competitive Programming Course aff. How to Use Recursion — Programming Tutorial coding. View More. Sagar Bhatia. Table of Contents. What is Procedural Programming? Procedural Programming. Sagar Bhatia Sagar is an engineering graduate and a technology lover and has been writing across various disciplines for over 5 years now. Leave a comment.

Submit Cancel. Maurice Daloshen from Kenya. Edge Dev Studio. Welcome Back. Forgot Password. Welcome to Hackr. Create Account. Send Password. The computer processors provide hardware support to procedural programming through a stack register and also provide instructions for calling procedures and returning from the stack.

Procedural programming languages are also imperative languages to make explicit references to the state of the execution environment. The major difference between these is procedural programming depends on blocks and scope whereas imperative programming may have or not have these features. The objective of procedural programming is to break down a program into a collection of variables, data structures whereas the main aim of object-oriented programming is to break down a programming task into objects.

In simple words, procedural programming uses procedures to operate on data structures, while object-oriented uses objects for the purpose. Based on Characteristics : Procedural programming has Local variables, sequence, selection, iteration, and modularisation. Object-oriented programming has Objects, methods, message passing, information hiding, data abstraction, encapsulation, polymorphism, inheritance, serialisation-marshalling.

Accessing modes : In Object-oriented programming, there are three accessing modes — Public, Private, and Protected. There are no such access modes in Procedural programming.

Execution : In Object-oriented programming, various functions can execute simultaneously. In procedural programming, there is a systematic approach in which functions get executed step-by-step. Data Control : In Object-oriented programming, data and functions are accessible within the same class while in procedural programming, data can move freely.

Security : Object-oriented programming is more secure than procedural programming, because of the level of abstraction or we can say data hiding property. It limits the access of data to the member functions of the same class. While there is no such data hiding in the procedural programming paradigm. Process : Object-Oriented programming follows the bottom-up approach while Procedural programming follows the top-down approach while designing a program.

Division : In Object-oriented programming, the program is divided into small entities called objects whereas in Procedural programming the program is divided into sub-procedures.

Procedural programming major disadvantage includes lack of code reusability. Programmer needs to type the same code at many applications which adds to the time and redundancy in a project. Object oriented programming needs mastery.

Programs developed using object oriented programming approach tends to be slower in execution than those developed using procedural programming because object oriented programming needs more number of instructions to execute in a program. Both the approaches are good to go and depends upon an individual or team which approach they prefer.

Object-oriented and procedural are high-level programming paradigms to solve problems in less time by writing modular code.

But OOP is best when it comes to bigger applications as procedural is not good for complex applications. We hope this article was informative and helped you gain more insights about Procedural and Object-Oriented Programming. To explore more articles, visit our blog page today. About Us. Privacy Policy. Bug Bounty. Press Release.



0コメント

  • 1000 / 1000