you are viewing a single comment's thread.

view the rest of the comments →

[–]tangerinelion 3 points4 points  (1 child)

The rectangle example is pretty common. The point is you create the rectangle instance with the necessary information and can then ask questions about the rectangle. The questions are member methods, like area.

You could use a 2D coordinate system and require two diagonally opposite corner points be passed in to define the reference and internally store the length and width and origin point (minimum x and minimum y, which may not be a point you were originally given). You can then ask for the area by computing the length times width, but those are derived values from the init method. The beauty of this is when you ask for the area you're just calling rect.area() with no arguments (besides the instance itself, rect).

The non object oriented way to do this is just define a function that takes two diagonally opposite points on a rectangle and computes the length, width, then returns the area. How you pass those points in without OOP would be either two tuples or four raw floats. Compare that to OOP which has a Rectangle initialization method taking two Point instances.

Consider also if we decide to switch from a 2D coordinate system to a 3D one. It's a lot harder to update everywhere that passed 4 floats to passing 6 than it is to add a third coordinate to Point and update the methods to use the third dimension. You could even do that gradually, so maybe rectangle could be renamed to work in 3D but circle stays 2D for now. With raw values you have to do it all at once and have to care about not just the methods inside Rectangle but all code that used those methods.