I still find with the examples used for teaching OOP counterproductive

After the stupid example with the taxonomy of the species/car/employees that makes you do a lot of inheritance one of the valid use case for OOP is 2D geometry : points & rectangles.

This is the python example in wiki.python.org

Don't worry, python is one of the many language doing this class. It is an actual use case for learning classes.

200 lines of code and you do nothing. Not even fun

Except in python complex is a base type, and I learned the use of complex numbers when I was  in public high school in my sweet banlieue. (a place said to be full of delinquents and uneducated kids)

Then I thought : with just simple math how hard would it be to just draw two polygons one of which rotated with the python base types? 
How hard is programming when you keep it simple?

Here is my answer in less than 10% of the initial number of lines of code and a drawing to illustrate how hard it is :

from PIL import Image, ImageDraw
# short constant names a la fortran
from cmath import pi as PI, e as E
I = complex("j")
to_x_y = lambda cpl: (cpl.real, cpl.imag)
im = Image.new("RGB", (512, 512), "white")
draw = ImageDraw.Draw(im)
rotation = E**(I*PI/3)
homothetia = min(im.size[0], im.size[1])
trans = homothetia/2
homothetia /= 2.5
polygone = (complex(0,0), complex(0,1), complex(1,1), complex(1,0), complex(0,0))
bigger = map(lambda x: x * homothetia + trans, polygone)
bigger_rotated = map(lambda x: x * rotation, bigger)
draw.line(map(to_x_y, bigger), "blue")
draw.line(map(to_x_y, bigger_rotated), "green")

im.save("this.png", "PNG")

I don't say classes are useless, I use them. But, I still have a hard time thinking of a simple  pedagogic example of their use. However, I see a lot of overuse of Object Oriented Programming to try to make people able to use concepts that are not accessible without basic knowledge. OOP is no shortcut for avoiding to learn math, or anything else. If you don't understand geometry and math, you will probably be whatever the quality of the class given unable to do any proper geometrical operations.

We need OOP. But the basic complex type far outweigh in usefulness any 2D geometry classes I see so far without requiring any dependencies.  And I never saw in 5 years complex types used even for doing 2D geometry, however it seems the alternative buzzword to OOP -including numpy- is so making you look data scientist and pro! 

So what is the excuse again for not using something that works and is included in base types with no requirements for doing geometry when we say that games could be a nice way to bring teenagers to coding? Are we also gonna tell them learning is useless and that reinventing the square wheel is a good idea? Are we gonna tell them to not learn because we have libraries for everything?

For the fun I did an OOP facade to complex as point/rectangle but seriously, it is just pedantic and useless. But still 50% less lines and more useful than the initial example.

No comments: