For quite some time I've been wondering if procedural programming has any advantages over OOP or if it's just personal preference. In which situations should I prefer the procedural style if there're any?
Edit: Deleted the poor example.
For quite some time I've been wondering if procedural programming has any advantages over OOP or if it's just personal preference. In which situations should I prefer the procedural style if there're any?
Edit: Deleted the poor example.
Sorry for the poor example. What I wanted to represent is that I have some data and a set of functions which operate on them.
Your examples aren't really procedural vs OOP. Also your first example does dynamic memory allocation while your second doesn't and honestly looks weird to me. It's hard to say something sensible about the pros and cons with short snippets of code. Here's Brian Will's channel where he rewrites reasonably sized programs from OOP to a procedural style. I'm not aware of anyone who does the opposite so it's one sided, but you can find some takeaways in there.
I'm actually running an experiment myself where I take on a more C style approach to programming and I have to say I'm very much liking it so far. I made a journal entry about it a while ago. One of the biggest advantages that I've personally noticed is that procedural code is much easier to read, but I'll get back on that once I'm further ahead in my journey.
I'm constantly moving away from OOP over the years. The idea of inheritance never made much sense to me - it just complicates things and forces you to make decissions about software design. To me that's just blah blah and i prefer to spend this time on solving real problems.
So i ended up using C with classes style, but i moved away from that too, mainly because of this:
Class member functions hide some of the data they use because you don't know what member variables they access without looking at the implementation.
This way it's hard to see data complexity, which is important to optimizing / refactoring.
Often i ended up making member functions static, forcing me to add all data to the function parameters - just to see how many there are (ALWAYS more than you would expect).
Next i realized that static member functions can be used from anywhere, how practical.
So why did i still using classes?
My answer was simply: To group related functions together by 'topic', so i cand find them somehow.
But there is something better to do this: Namespaces.
With namespaces it's possible to group stuff in hirarchies without any restrictions or problems known from inheritance.
Today i create a classes very rarely, using them only as an interface to a large system which is implemented mostly procedural under the hood.
But i still use a lot of small structs with member functions for trivial functionality like indexing arrays or un/packing.
I am the same as JoeJ, I now prefer procedural and also functional (Not the same thing), I feel I get more reuse and easier composition
In the end it does not matter if you use OO, procedural or functional, a good design is a good design. So use whatever works for you, or better still use the one that best fits your needs :)
I'm constantly moving away from OOP over the years. The idea of inheritance never made much sense to me
For quite some time I've been wondering if procedural programming has any advantages over OOP or if it's just personal preference. For example:
Your example isn't actually procedural programming at all. Instead it's an attempt to mimic the look of a C++ class in C-style code, but without any of the benefits that actually using a C++ class brings. This isn't actually something you would do; procedural programming is a very different paradigm.