Object Oriented Programming vs Structural Programming

In summary, OOP organizes code more efficiently grouping functions and variables that are "similar" into an object. This makes it easier to use an object and to make changes to it.
  • #1
fog37
1,568
108
Hello,

I am trying to get my head around the important difference between these two different styles (paradigms) of programming. I am familiar with traditional structural programming: data, variables, functions, structures. The functions are blocks of instructions that can process data, etc.
OOP is said more centered around the data than structural programming. I understand there are classes, objects, instances, methods, etc...

While programming in Arduino, I noticed that the use of certain hardware components, like a servo motor, has OOP in the code but I am not sure how it truly works, why it is needed and its advantages...

Thanks!
Fog37
 
  • Like
Likes harborsparrow
Technology news on Phys.org
  • #2
 
  • Like
Likes harborsparrow
  • #3
As the name implies, OOD programming has "objects" as it's central feature. An object may treat data very differently, but I would say that the object, not the data is central. A good example may be the simulation of a traffic intersection. A traditional approach may be to make an array of vehicles and their properties and follow them through the intersection. Making it more OOD would be to have something create vehicle objects which initialize all their properties when they enter the simulation and control access to their properties through the simulation till they leave the intersection. A car object may know that it has bad brakes and behave as such. The other vehicles are not aware of the brake problem, and can only detect the speed and behavior of that car. Another object may be a motorcycle. It would have different stearing, acceleration, and braking procedures and behave accordingly. A third object might be an 18-wheeler truck. It would have very different procedures, properties, and behavior . These objects all take care of their own behavior and data, only allowing outsiders to know things and interact in restricted ways. This approach can be very helpful in organizing a computer program. (It can also be a great nuisance if it is overdone.)
 
  • Like
Likes harborsparrow
  • #4
As FactChecker said, the data is NOT central to OOP, it is one of two equally important parts of OOP, namely the data and the methods, which work hand in hand. As anorlunda said, OOP is a massive subject.

Study the 3 fundamentals to start with.
  • Encapsulation
  • Inheritance
  • Polymorphism
Nothing we tell you is going to really mean much until you have at least a basic understanding of those concepts
 
Last edited:
  • Like
Likes harborsparrow, QuantumQuest, atyy and 1 other person
  • #5
I have done some reading. It is possible to write a program with many functions using the conventional procedural programming paradigm. Corrections are highly welcomed!

My understanding is that OOP organizes code more efficiently grouping functions and variables that are "similar" (related) into an object.

The functions within an object become called methods and the variables within the object are called properties.

functions= methods
variables =properties
object = example (instance) of a class
class= objects with common variables and properties

The term encapsulation is a fancy way to describe the idea that related variables and the functions that use those variables are grouped together into a unit called object. In regular procedural programming, the variables and the functions using those variables are in the same code but act more as separate entities.

Code written in OOP can have methods, i.e. functions, that do something but have few or even no input parameters when compared to the same functions used in procedural programming. This is because the method in OOP relies on the properties (variables) within the object...
Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?

An interesting feature of OOP is abstraction: this makes it easier to use an object. It is about by hiding (making abstract) some of its properties and methods within the object itself so the user only interfaces with certain methods and functions and does not need to care about the other variables and methods inside the object .
Abstraction also makes changes to the object easier to implement without affecting the rest of the code outside of the object.

There is then inheritance which means that several objects can refer to a particular different object (which has its own properties and methods) and "inherit", i.e. refer to and use, the properties and methods defined for this particular reference object without having to redefine all those same properties and methods.

Polymorphism: not fully understand this yet.

Let me know if I am on the right track. In reference to my question above, I would like to understand how a function can do something without inputs or outputs...

Thanks!
 
  • Like
Likes harborsparrow
  • #6
Methods are functions. They can get arguments and they can return results.

What is your goal, and how are you studying?

If you want to learn OOP, then I suggest a tutorial that includes coding examples. Based on your questions, it sounds like you are reading definitions without coding examples.
 
  • Like
Likes harborsparrow
  • #7
anorlunda said:
If you want to learn OOP, then I suggest a tutorial that includes coding examples. Based on your questions, it sounds like you are reading definitions without coding examples.
what he said (very small).jpg
 

Attachments

  • what he said (very small).jpg
    what he said (very small).jpg
    3.2 KB · Views: 615
  • Like
Likes harborsparrow
  • #8
fog37 said:
Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?

Let me know if I am on the right track. In reference to my question above, I would like to understand how a function can do something without inputs or outputs...
Yes. You are on the right track. A method can have access to the data of the object. It can use some as inputs and modify some as outputs. It can also perform other tasks, such as initialize a buffer of data, check the status of some hardware, signal other entities that the object exists, etc.
 
  • Like
Likes harborsparrow
  • #9
fog37 said:
My understanding is that OOP organizes code more efficiently grouping functions and variables that are "similar" (related) into an object.

More generally object-oriented design (OOD) which provides a more natural and intuitive way to view the design process, is modeling software components just as we describe real-world objects i.e. by their attributes and behaviors. Also, OOD models communication between objects. Objects communicate via messages just as people do for instance.

fog37 said:
The term encapsulation is a fancy way to describe the idea that related variables and the functions that use those variables are grouped together into a unit called object. In regular procedural programming, the variables and the functions using those variables are in the same code but act more as separate entities.

It rather refers to the concept of information hiding which is crucial to good software engineering.

When we design an object - and next implement it in an OOP language, we encapsulate attributes and operations (behavior) into objects. The attributes and operations of an object are intimately tied together. Now, objects have the property of information hiding. This means that, although objects may know how to communicate with one another across well-defined interfaces, objects normally are not allowed to know how other objects are implemented as implementation details are hidden within the objects themselves.

fog37 said:
Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?

Methods in an OOP language and functions in a procedural language have essentially the same functionality as both are called by their name and operate on data but in the case of a method, it is associated with an object. Now, a function may be passed data on which it operates (input parameters) and may return data i.e. some return value . When a method is called, it is implicitly passed the object on which was called and it operates on this so we also have the notion of a class here which is basically a blueprint for the aforementioned object.

Now, a void function performs a task, and then control returns back to the caller but, it does not return a value. As for functions having no inputs that use "external" variables as you say, you most likely mean functions operating on global variables which is a fairly dangerous programming practice. Global variables are visible i.e. can be used from any part of a program, so this does not refer exclusively to functions.

fog37 said:
An interesting feature of OOP is abstraction: this makes it easier to use an object. It is about by hiding (making abstract) some of its properties and methods within the object itself so the user only interfaces with certain methods and functions and does not need to care about the other variables and methods inside the object .
Abstraction also makes changes to the object easier to implement without affecting the rest of the code outside of the object.

Just to make it a little more clear, in the real world we, humans, think in terms of objects and we possesses the marvelous ability of abstraction. For instance, we can view a mountain in a digital image as object rather than as individual dots of color. In the OOP context, objects provide an abstraction which hides the internal implementation details. You are given a "handle" which are the methods that the object exposes to the programmer in order to use it but you don't have to know about these methods' implementation details i.e. how they produce the desired result.

fog37 said:
There is then inheritance which means that several objects can refer to a particular different object (which has its own properties and methods) and "inherit", i.e. refer to and use, the properties and methods defined for this particular reference object without having to redefine all those same properties and methods.

Inheritance is a form of software reuse in which classes are created by absorbing an existing class's data (attributes) and methods (behaviors) and embellishing them with new or modified capabilities. I'm referring to classes because these are the blueprints from which the objects are instantiated. The benefits of inheritance from a software engineering perspective are both saving time during program development through software reusability and also reuse of proven high quality software.
 
  • Like
Likes Klystron, harborsparrow, FactChecker and 1 other person
  • #10
My two cents worth. (And, i like the previous comments people have made as well).

When I taught programming, I emphasized that creating an object (in object-oriented languages) allocates memory for the object, and destroying the object returns the memory to the open pool. Scope rules generally apply, so unless the object is "static" (similar to "global" in procedural languages), only code which has the object name (which is really a pointer in disguise) "in scope" can use the object. By "use the object", I mean access/read/write the data stored within the object.

I emphasized the memory usage because, unless you know what is going on in memory (at least in a virtual sense), eventually a person will have problems using ANY programming language, especially for code used on the web.

An object definition is a description of a data structure. If an object "inherits" from another object definition, it is its progenitor's data structure, plus whatever you add to it.

Note that inheritance also is used in C++, which is largely a procedural language, whereas inheritance is an object oriented concept. So C++ is a particularly large, tricky, messy language (though powerful of course), mainly because it is pretty much the only OOPL that allows for multiple inheritance, which can because so twistedly complex that it can become difficult for the programmer to understand. As a result, later OOPL's restricted objects to inheriting from a single other object. And of course, C++ is like C in that it really does not shelter you at all from the responsibility of knowing almost precisely what is happening in the computer's memory.

Object-oriented programming languages, OTOH, try to manage memory "safely" for you, so that you are free to think more conceptually about your programming goals. As long as performance is not a paramount requirement, as a learner, you can sort of kind of ignore memory allocation. For a while. But it is better not to, especially if you have already learned C first. But Java or C# will certainly shield you from a portion of the nitty gritty details going on in executing the program at run-time--up to a certain point.

So if you've been exposed to C or C++, and pointers, then just realize that in Java or C#, an object name is a pointer variable in effect. When you "instantiate" (create) the actual object, the programming environment populates the address of the newly allocated object into that object name (pointer variable). When you destroy an object by setting it to null, its allocated memory should considered reusable by the system. It isn't quite as simple as that, so if performance matters over time, you'll want to learn much more about how object-oriented languages such as Java or C# handle memory allocation and de-allocation internally (i.e., there is an automatic garbage collector, but you NEVER want to actually let it run, if performance matters, because it temporarily takes over the processing from your program while it runs--a situation that is preventable by always carefully destroying your objects after you are done using them (and before you set them to null); this is an advanced topic though).
 
Last edited:
  • Like
Likes Klystron, phinds, QuantumQuest and 1 other person
  • #11
There is one aspect of OOD that is simple and IMHO has great practical advantages. When one includes several libraries, there has always been a problem when multiple libraries might have functions with the same name. The problem is to control which library function is used. OOD allows one to control that. So using external libraries much easier when the programmers have used OOD.
 
  • Like
Likes harborsparrow
  • #12
FactChecker said:
There is one aspect of OOD that is simple and IMHO has great practical advantages. When one includes several libraries, there has always been a problem when multiple libraries might have functions with the same name. The problem is to control which library function is used. OOD allows one to control that. So using external libraries much easier when the programmers have used OOD.
Are you referring to the concept of namespace, where you qualify the use of some identifier by the namespace it belongs to? If so, I'm not sure that is specific to OOD. I could be wrong, though.
 
  • Like
Likes FactChecker and harborsparrow
  • #13
Mark44 said:
Are you referring to the concept of namespace, where you qualify the use of some identifier by the namespace it belongs to? If so, I'm not sure that is specific to OOD. I could be wrong, though.

Maybe namespaces helping distinguish libraries is sort of a side effect of OOP.

Of course, name collisions do still happen in OOP even when a namespace is required. I blogged about an annoying one here: http://harborsparrow.blogspot.com/2012/11/microsoft-update-kills-web-application.html

My website was about algae studies, and algal samples taken from specific "sites". So I had an object (page) named Site that displayed everything known about one sampling site--as I'm sure biologists and geologists world around do. And some idiot at Microsoft decided to introduce Site as a namespace into its .NET library suite. It broke my web service without warning, and it took me a few hours to get the web service working again (and a lot of painful recoding).
 
  • Like
Likes FactChecker
  • #14
anorlunda said:
Methods are functions. They can get arguments and they can return results.

What is your goal, and how are you studying?

If you want to learn OOP, then I suggest a tutorial that includes coding examples. Based on your questions, it sounds like you are reading definitions without coding examples.
Any tutorial recommendations that you particularly like? Simple and gradual is highly desirable by me.
 
  • #15
Chestermiller said:
Any tutorial recommendations that you particularly like? Simple and gradual is highly desirable by me.
Chet, take a look at this book -- https://www.amazon.com/dp/B00F9311YC/?tag=pfamazon01-20
I looked at 20 or 30 books on Amazon, but didn't see any names that I had recognized from years back when I taught C++ (other then Herb Schildt, who has received scathing criticism for the errors in his books).
The author of the book I linked to, Alex Allain, has a website called cprogramming.com. I've looked at a number of his articles on that site, and they seemed quite good. He also has tutorials for C and C++ on his site, so you might start there. If you like what he has to say, you might consider getting the book, too. It's relatively inexpensive, at about $35, cheap as programming books go.
 
  • #16
Thanks Mark. I'll take a look. My main interest is C#, and I've had some experience with learning and solving practice problems with that until I ran into OOP. Then, head spinning. Any C# recommendations that make OOP simpler?
 
  • #19
Thank you everyone. I am getting some concepts clear about OOP.

As far as a function being void, returning a parameter, etc. I am used to think of a function as receiving one or multiple input that it will use and process to produce an output: ##output = f(x_1, x_2, etc.)## If the function is void, it means it does not receive any parameter to work on. For example, in the Arduino programming language,
there is function loop() which is void. But lots of activities happen inside the loop() function...

In C, for example, it seems that the return instruction can only provide a single output value. What if a function needed to manipulate multiple inputs to produce multiple outputs? Having the return function with only a single output seems limiting. Where am I wrong?

Thanks.
 
  • #20
fog37 said:
In C, for example, it seems that the return instruction can only provide a single output value.

But that output value can be a pointer, which can point to anything--it can point, for example, to a structure containing multiple fields (and some of those fields can in turn be pointers to still other structures, etc.).

fog37 said:
What if a function needed to manipulate multiple inputs to produce multiple outputs?

Then it would store the multiple outputs in a structure and return a pointer to it. Or, which is a much more common technique, one of the inputs could be a pointer to a structure that the function will store its outputs in. (With this technique, the function is usually either void or returns a value indicating whether it succeeded or failed at its task.)
 
  • Like
Likes QuantumQuest and Klystron
  • #21
fog37 said:
If the function is void, it means it does not receive any parameter to work on.

No, that's just a function with no parameters. A void function is a function that returns no result (except that, as I said in my previous post just now, it can still receive a pointer to a structure that it stores results in). At least, that's the C terminology.
 
  • Like
Likes QuantumQuest
  • #22
fog37 said:
If the function is void, it means it does not receive any parameter to work on.

PeterDonis said:
No, that's just a function with no parameters. A void function is a function that returns no result (except that, as I said in my previous post just now, it can still receive a pointer to a structure that it stores results in). At least, that's the C terminology.
Just to clarify, a void function does not return a result. If one of its parameters is a pointer (to, say, a structure), when the function returns, the structure that is pointed to will have been updated with new values. In this case, the structure is being passed by reference, as opposed to being passed by value. Technically, the function doesn't return any value, so qualifies for being a void function.
 
  • Like
Likes QuantumQuest
  • #23
Thank you very much everyone. OOP has become more clear.

Let me post this short Arduino code:
Screen Shot 2019-03-05 at 8.46.04 AM.png

The loop function does not take any input parameter and does not output any parameter. However, various simple calculations and actions (print, etc.) are performed within its curly brackets. The function loop may not take any input parameters but it uses the variables and data outside its scope...

So, when a function uses data defined externally to it, it still does not mean that is it receiving parameters. For example, let's think of a function performing a calculation, with a certain formula, using values that stored in variables outside of the function. Are those values parameters? What about the result of the calculation? Would that be the output of the function or not necessarily?
 

Attachments

  • Screen Shot 2019-03-05 at 8.46.04 AM.png
    Screen Shot 2019-03-05 at 8.46.04 AM.png
    9.3 KB · Views: 602
  • #24
"parameters" usually refers to the specific data that is passed in as part of the function call. Data obtained another way are not called parameters. When external data is modified which has not been accessed through the parameter list, that is called a "side effect". Many people hate side effects because it makes the effect of the code more difficult to keep track of. The parameter list is where people will look first to see what a function does. It is also a way that specific functions with the same name but with different parameter lists can be distinguished from each other. When two functions have the same name, there are programming tools which use the parameter list to help the programmer decide which function he wants to use.
 
  • Like
Likes QuantumQuest
  • #25
FactChecker said:
The parameter list is where people will look first to see what a function does. It is also a way that specific functions with the same name but with different parameter lists can be distinguished from each other. When two functions have the same name, there are programming tools which use the parameter list to help the programmer decide which function he wants to use.
The mechanism discussed above is called function or method overloading, and pertains to C++, C#, Java, and possibly other languages. It does not pertain to C.
 
  • Like
Likes FactChecker
  • #26
Mark44 said:
The mechanism discussed above is called function or method overloading, and pertains to C++, C#, Java, and possibly other languages. It does not pertain to C.
Good point. And I am not sure that it is really part of OOP or just a common feature of more modern languages.
 
  • #27
FactChecker said:
specific functions with the same name but with different parameter lists can be distinguished from each other.
In the case of C++, this is usually accomplished by changing the internal function names to include parameter information, known as name mangling. A similar thing was done for legacy 16 bit X86 C compilers, where the actual library function names in a C source file were modified depending on the model (tiny, small, medium, large, huge). However, assembly code had to use the actual modified function names.

https://en.wikipedia.org/wiki/Name_mangling

Getting back to OOP, there's the option of using a static member function when dealing with two (or more) objects, such as a compare function. Assume there is a class named xmpl_class, and two objects, xmpl_class a, b. If the compare function is non-static, the usage would be a.compare(b) or b.compare(a). If the compare function is static, the usage would be xmpl_class::compare(a,b). There are also static member variables, which other than syntax, are similar to regular static variables.

There's another type of programming, which I call associative which is similar to declarative, dating back to the days of plug board programming and the language RPG II which was derived from plug board programming. Actual plug board systems used card readers and line printers, RPG II could also use files. Most of the programming defined links between input fields and formatted output fields or a limited set of math functions, where an input field would be linked to a partial or total summation function. The order of operations was not defined (in the case of plug boards, it's just a bunch of wires, which in theory could have allowed all of those operations to occur in parallel).

https://en.wikipedia.org/wiki/Plugboard

https://en.wikipedia.org/wiki/IBM_RPG_II
 
Last edited:
  • Like
Likes FactChecker
  • #28
In OOP, when we see an instruction like Servo myservo, the word myservo is an arbitrary name given to the object (an instance of the class) while Servo is the class itself, correct? The line of code Servo myservo creates the object.
On the other hand, I believe that commands like myservo.attach(pin) and myservo.write() are methods contained in the Servo library that are used by the object myservo, correct? So when we see two words separated by a period, the first is the object and the second is the method. As an other example, the command serial.begin() used to initiate serial communication: the method is begin which is used by the object serial. The object serial is an instance of a certain class...
 
  • #29
fog37 said:
In OOP, when we see an instruction like Servo myservo, the word myservo is an arbitrary name given to the object (an instance of the class) while Servo is the class itself, correct?
Yes
fog37 said:
The line of code Servo myservo creates the object.
On the other hand, I believe that commands like myservo.attach(pin) and myservo.write() are methods contained in the Servo library that are used by the object myservo, correct?
Yes, although I wouldn't call Servo a library. Servo is a class that exposes the write() and attach() methods, and whatever other methods that are defined on the Servo class.
fog37 said:
So when we see two words separated by a period, the first is the object and the second is the method.
It doesn't have to be a method. A public property or field can also be accessed this way.
fog37 said:
As an other example, the command serial.begin() used to initiate serial communication: the method is begin which is used by the object serial. The object serial is an instance of a certain class...
 
  • Like
Likes FactChecker
  • #30
Syntax depends on the language. In VB, you would expect to see something like:

Public myControlArray As classLabelArray

Where classLabelArray is a defined class and the statement makes myControlArray in instance of the class. In VB, a raw statement like

classLabelArray myControlArray

Would be gibberish.
 
  • #31
Thank you. You are right. I guess I was my question was implicitly referring to the Java language...
 
  • #32
"The line of code Servo myservo creates the object."

I think the above statement is not precisely accurate. "Servo myservo" declares the name-and-type of an object that the programmer intends to use subsequently. Actually to create (i.e., allocate memory for) the object requires an assignment statement, such as "myservo = new Servo".
 
  • Like
Likes QuantumQuest and Chestermiller
  • #33
OOP reflects the real world behavior of how things work. It is important because
  • It make visualization easier because it is closest to real world scenarios.
  • You can reuse the code, this saves time, and shrinks your project.
  • Once you know the concept, it makes you a cooler coder among your friends ;) .
There are 3 basic features of object oriented programming language:
  • Inheritance
  • Polymorphism
  • Encapsulation
By using these features you can easily reuse your program or part of your program. Object oriented programming provides you a power to do programming in a very effective manner. Learn OOP concept and implement it in your daily programs.
 
  • Like
Likes FactChecker

1. What is the main difference between Object Oriented Programming (OOP) and Structural Programming?

The main difference between OOP and Structural Programming lies in their approach to organizing and managing code. In OOP, code is organized into objects that have attributes and behaviors, while in Structural Programming, code is organized into procedures and functions that manipulate data.

2. Which programming paradigm is more suitable for large-scale projects?

OOP is generally considered more suitable for large-scale projects due to its ability to break down complex systems into smaller, more manageable objects. This allows for easier maintenance, scalability, and code reuse.

3. Can you mix OOP and Structural Programming in a project?

Yes, it is possible to mix OOP and Structural Programming in a project, but it is not recommended as it can lead to code complexity and difficulty in maintenance. It is best to choose one programming paradigm and stick to it throughout the project.

4. Which programming paradigm is more efficient in terms of performance?

Structural Programming is generally considered more efficient in terms of performance as it does not involve the overhead of creating and managing objects. However, with advancements in technology and optimization techniques, the performance difference between OOP and Structural Programming has become negligible.

5. What are the main advantages of using OOP over Structural Programming?

OOP offers several advantages over Structural Programming, including code reusability, easier maintenance, scalability, and better organization of code. It also allows for the implementation of real-world concepts such as inheritance, polymorphism, and encapsulation, making it a more powerful and flexible programming paradigm.

Similar threads

  • Programming and Computer Science
Replies
22
Views
907
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
10
Views
2K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
24
Views
3K
  • Programming and Computer Science
Replies
26
Views
4K
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top