When I am presented with programming problems, I naturally start breaking them up into logical objects in my head. Who has what responsibility, who owns what, who derives from what, etc.
I am struggling with C. I just don't get how to do things in a Procedural Language.
Can an experienced C programmer help explain how I should think about my programs during design time?
For example, I want to write my own Semaphore class. I would naturally need a Queue data structure for my program, which I would like to write myself as well. If I needed to do this in Java or C#, I could simply whip up a quick Queue class and create a new instance of it in my Semaphore class.
But in C, there aren't objects. So do I have to inline all the behavior of my Queue data structure?
Can someone help me "get it"?
Related: what is the best way to plan and organize development of an application in c
As a beginner, OOP is also more difficult to read for several non-code related reasons. First, it's near impossible to understand why a piece of code exists if you're unfamiliar with the domain being modeled with classes. Secondly, OOP is a craft and is inherently opinionated.
Object-oriented programming has fulfilled many of its promises. Software systems today are longer-lived and more amenable to change and extension than ever. Nevertheless we observe that object orientation is slowly dying, with the introduction of ever more complex and heterogeneous systems.
Nondeterminism inherent in OOP programs makes the code unreliable.” As the program executes, its flow can take many, many different paths — thanks to all of those different objects, with new objects sometimes even created on-the-fly.
Students find it very difficult to understand object oriented concepts like classes, constructor invocation, overloaded constructors, friend functions and other object oriented concepts [2]. Students who have been exposed to procedural programming find it a little difficult to move towards object oriented programming.
But in C, there aren't objects. So do I have to inline all the behavior of my Queue data structure?
No.
Do this.
Define your class however you feel comfortable doing OO design.
Write the attributes of your class as a C-language struct.
Put that struct in a header file, along with all of the functions that operate on that struct. Make sure a MyStruct * self
is the first argument to all of these "method functions".
Write a C module with all of the bodies of the method functions.
Poor-person's OO in C. It works well. Just be disciplined about putting everything into the struct that you need -- public and private instance variables -- everything.
Generally, avoid trying to have private variables in the first place. You don't have the full power of an OO compiler, so don't bother with low-value features like "private" or "protected".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With