M
Manuel
Please, excuse me for starting this discussion.
I'm not a troll, and I love the OOP architecture.
However I'm a C++ newbie, and sometimes it's hard for me to fully
understand the improvement of OOP. Please excuse my poor english too.
I hope it's sufficient to explain my question.
Generally OOP is better than procedural, but in a case I'm working on,
I'm not sure. This is an example of my doubts.
I'm writing a simple GUI openGL based.
I've some buttons, that must call some functions.
PROCEDURAL STYLE
I assume that:
-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This
callback check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID
The procedural code is very simple (it's pseudo code):
------------------------------------------
//declare some functions
myfunction1(string)
myfunction2(int)
myfunction3(string, int)
//declare some buttons:
new Button("foo1");
new Button("foo2");
new Button("foo3");
//the mouse callback function for GLUT
mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------
that's all.
What I see:
- It's flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It's readable and easy, even for a newbie.
- It don't need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
However, I know, it's not fully OOP. So some coders, experts in C++,
tell me that I should use functionoids. So I'm starting from here:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10
but I don't understand why functionoids (in this case) is better.
I've not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.
1) I must write a base class with a pure-virtual method
2) I must write a derived classes for each function (so if I've 100
functions, I must write 100 derived classes ?)
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this
mean a lot of difficults, hard debug, possible memory lacks or dangling
references.
Note that I must write, in both cases, the same number of functions.
But in second case, it's not sufficient write a simple function, but
it's needed insert it in the functionoid pattern...so I'm almost sure
that the lines to write is increased in second case.
However, the doubt is not only about the increase of complexity, but
about the advantages of functionoids (alway, in this particular case);
again, I admit to have not fully understand functionoids, but seem that
advantages is about the arguments that is possible to pass:
"Then later you pass the remaining args using that pointer, and the
system magically takes your original args (that were freeze-dried),
combines them with any local variables that the function calculated
prior to being freeze-dried, combines all that with the newly passed
args, and continues the function's execution where it left off when it
was freeze-dried. "
This sound good! But I've all flexibility I need with procedural
approach: the buttons can call any type of function, with any type and
number of parameters, mixing local variable with widgets variables...
To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from
"inheritance indulgence"”- for example, many multiple inheritance
designs evolve by insisting that inheritance be used everywhere.
One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------
So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?
Please, remember I'm only a newbie, and maybe I can't see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don't require complex functionoids and I don't know
because my ignorance.
Again, I'm not a troll...only I want understand deeply because all tell
me I must use, for this simple GUI, a different approach, not "switch
based".
I hope to don't boring you, and please, don't reply me so hard...
regards,
Manuel
I'm not a troll, and I love the OOP architecture.
However I'm a C++ newbie, and sometimes it's hard for me to fully
understand the improvement of OOP. Please excuse my poor english too.
I hope it's sufficient to explain my question.
Generally OOP is better than procedural, but in a case I'm working on,
I'm not sure. This is an example of my doubts.
I'm writing a simple GUI openGL based.
I've some buttons, that must call some functions.
PROCEDURAL STYLE
I assume that:
-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This
callback check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID
The procedural code is very simple (it's pseudo code):
------------------------------------------
//declare some functions
myfunction1(string)
myfunction2(int)
myfunction3(string, int)
//declare some buttons:
new Button("foo1");
new Button("foo2");
new Button("foo3");
//the mouse callback function for GLUT
mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------
that's all.
What I see:
- It's flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It's readable and easy, even for a newbie.
- It don't need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
However, I know, it's not fully OOP. So some coders, experts in C++,
tell me that I should use functionoids. So I'm starting from here:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10
but I don't understand why functionoids (in this case) is better.
I've not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.
1) I must write a base class with a pure-virtual method
2) I must write a derived classes for each function (so if I've 100
functions, I must write 100 derived classes ?)
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this
mean a lot of difficults, hard debug, possible memory lacks or dangling
references.
Note that I must write, in both cases, the same number of functions.
But in second case, it's not sufficient write a simple function, but
it's needed insert it in the functionoid pattern...so I'm almost sure
that the lines to write is increased in second case.
However, the doubt is not only about the increase of complexity, but
about the advantages of functionoids (alway, in this particular case);
again, I admit to have not fully understand functionoids, but seem that
advantages is about the arguments that is possible to pass:
"Then later you pass the remaining args using that pointer, and the
system magically takes your original args (that were freeze-dried),
combines them with any local variables that the function calculated
prior to being freeze-dried, combines all that with the newly passed
args, and continues the function's execution where it left off when it
was freeze-dried. "
This sound good! But I've all flexibility I need with procedural
approach: the buttons can call any type of function, with any type and
number of parameters, mixing local variable with widgets variables...
To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from
"inheritance indulgence"”- for example, many multiple inheritance
designs evolve by insisting that inheritance be used everywhere.
One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------
So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?
Please, remember I'm only a newbie, and maybe I can't see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don't require complex functionoids and I don't know
because my ignorance.
Again, I'm not a troll...only I want understand deeply because all tell
me I must use, for this simple GUI, a different approach, not "switch
based".
I hope to don't boring you, and please, don't reply me so hard...
regards,
Manuel