Duplication of code --- necessary?

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.

···

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
···

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?