Blah said:
Show me something without OOP that isn't spaghetti code.
Is that what they taught you in university? There's a huge gap between unstructured code and OOP (which some might consider over structured code). In fact, very little would qualify as genuine spaghetti code, but to be so it would usually at least need to have some gotos. Even assembly code full of gotos doesn't necessarily have to be spaghetti code, although what your compiler generates probably will be.
QUOTE
If you do well organized, structured C with black box functions of only one entry and exit point you're already in OOP.
That isn't OOP. Contrary to popular misconception OOP isn't about encapsulation. The heart of OOP is inheritance and polymorphism. Encapsulation is generally a good practice of course.
You can do OOP in C by casting structures and fiddling around with function pointer tables for vtables. This isn't actually worth it most of the time, maybe if you have one level of inheritance without virtual functions.
QUOTE
On the other hand if you call return every three lines you've got spaghetti. That is part of the beauty of C.
Having multiple returns in a function isn't spaghetti. It's entirely subjective which is easier to read, but I can tell you for sure that having a single return instead of wrapping the coming lines in an if will usually be easier for me to understand (and it'll be easier for the compiler to not screw up performance wise). This is just another one of those things that are taught out of a sense of ideological purity, ie, dogma...
QUOTE
The problem with OOP is that I don't even understand its simplest concepts, like objects, or classes. Not to mention I don't like the concepts of garbage collection or operator overloading. I guess that might be a problem if ever I'm required to program in OOP.
I wish I could help you, I'd love a systems job where I did C and ASM (although I do a lot of C now, as well as C like C++, I also do Java and real C++ too)..
Learning the basics of OOP won't be hard for you. First, ignore other language features like garbage collection or operator/function overloading.
Think of a class as being based upon the idea of what a struct is. They define a new datatype. A struct only defines what pieces of data constitute that datatype, but a class also defines the operations you may do on that datatype. This means the functions that use that datatype's data. These functions are called "methods", and they are automatically passed an instance of that class (called an object) as a hidden parameter. This will probably be clearer with an example:
Take the stack data type (say it's a stack of integers). You could implement this with an integer array and an integer for the stack pointer. So in C it'd be like:
typedef struct
{
int stack_data[STACK_MAX];
int stack_pointer;
} stack;
Then you have functions for initializing the stack and pushing/popping elements. I'll just do the push here for sake of brevity.
void stack_initialize(stack *s)
{
s->stack_pointer = -1;
}
void stack_push(stack *s, int element)
{
s->stack_pointer++;
s->stack_data[s->stack_pointer] = element;
}
For a class, you'd define it like this:
class
{
int stack_data[STACK_MAX];
int stack_pointer;
stack()
{
stack_pointer = -1;
}
push(int element)
{
stack_pointer++;
stack_data[stack_pointer] = element;
}
} stack;
There are a couple things to note here: the initialization function was renamed to the name of the class. That's a "constructor" which is called when an object of that class is created. So when you do:
stack s;
Then the constructor is automatically called.
Then you can do:
stack.push(5);
Another thing to note is that within the methods all of the data of the class (called instance variables, or member variables) are accessible directly (so they fall within the scope).
This only describes the basics of classes, which is really pretty simple syntactic style over C. It doesn't explain inheritance and polymorphism which like I said are the heart of OOP (IMO) but it's a good start to understanding it all.
Oh yeah thanks, that's a very good explanation. Never understood what classes and constructors were before. Now that I understand that I can see how it's convenient.
Javacat said:
PS, I know you've developed a few pieces of audio software, but would you be able to give a little bit of info on your background? Age? University graduate? School dropout? 50 year old ASM programmer that has just lost their job and is feeling lost in the world?
I'm 21 and never really had a job before. And I've studied both programming and sysadmin in two separate schools, dropped out of the first one and I didn't yet get my degree from the second one, which is unsure whether or not I'll get it for various reasons. My
resume might make things clearer.