Duplication of code --- necessary?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Duplication of code --- necessary?

Virgil Stokes
I have been looking at the wxPython demos (which are excellent) and came across
something that bothers me a little. More specifically in the  PenAndBrushStyles
demo there are two classes, PenPanel and BrushPanel, and both contain the method
OnPaint. Much of the code in these two OnPaint methods is identical --- is this
duplication necessary?




--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Duplication of code --- necessary?

Metallicow-2


On Monday, January 20, 2014 5:26:10 AM UTC-6, Birdy wrote:
I have been looking at the wxPython demos (which are excellent) and came across
something that bothers me a little. More specifically in the  PenAndBrushStyles
demo there are two classes, PenPanel and BrushPanel, and both contain the method
OnPaint. Much of the code in these two OnPaint methods is identical --- is this
duplication necessary?


Not really, but it a simple demo. Therefore the user can easily copy the class out of the code.
A small bit of duplication sometimes makes things a bit more clear concise for beginners.

You could use the same basic reusable code in your own projects.
I alot of times will go over my code and cut out alot of it and convert it into a mixin or throw for example all reusable dc related drawing defs into a module of their own, so as to cut down on duplication.
Adding a single function call to do what is called other places more than twice is generally a good idea.

But also remember the more function calls you have while drawing something, Ex: *usually* happens often in a PaintEvent, then the slower it will run/perform also.
If you only need to draw something once or not often, then cutting reusable portions out is a better option.

If you are constantly updating/refreshing the GUI, then you will want less dots/calls/etc for performance.

It all depends on the situation at hand.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Duplication of code --- necessary?

Mike Driscoll
In reply to this post by Virgil Stokes


On Monday, January 20, 2014 5:26:10 AM UTC-6, Birdy wrote:
I have been looking at the wxPython demos (which are excellent) and came across
something that bothers me a little. More specifically in the  PenAndBrushStyles
demo there are two classes, PenPanel and BrushPanel, and both contain the method
OnPaint. Much of the code in these two OnPaint methods is identical --- is this
duplication necessary?


For a simple example in the demo, it's probably fine. In a regular program, you would take the duplicated code and put it in a regular function either in the source or a separate module.

- Mike

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.