Python Forum
Calculating surface area - - OOP or functional? Derek Banas Udemy course
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating surface area - - OOP or functional? Derek Banas Udemy course
#6
(Mar-12-2021, 09:57 PM)Drone4four Wrote: the same outcome can be achieved with 75% less code
Actually, your code is not replicating all the functionality of the original script. e.g. in original script you can change height/weight of the instance, they print the square attributes, their code is heavily commented (which amounts for big part of these extra lines), etc. i.e. you emulate just the outcome, not 100% of the functionality/level of documenting the code. Of course, their code can be refactored - get rid of redundant things like printing "Retrieving the height".

All that said, I agree with others who pointed out that their code is terrible. Like @Gribouillis said - it may be better to find another course, learning material.
Others have pointed enough of deficiencies, but here are some more:
  • Having a Square class that allows for different height and width is poor choice of class name, confusing and misleading, and a real math non-sense. Rectangle is what you have right now. One may have a Rectangle class like this one, then have e.g. Square class that inherits from it, that ensure equal sides. One may even have a Polygon or Shape class that Rectangle will inherit from. That is also an example of the benefit of OOP over your approach - extensibility.
  • height and width default values are strings and the values are expected and stored as str, converted every time when area is calculated. Even if they want to allow to instantiate the class by passing str as arguments, then they can convert inside the setter and store value stored as int, float or other numeric type. I understand they want to use str.isdigit for validation, but it will raise error if you pass int (and that is what I find way more logical to do, than pass str).
  • the dubious use of getters/setters was already mentioned, but I am ready to give them the benefit of the doubt - i.e. if they wanted to show it as an existing option (i.e. there are legit cases when it makes sense ti use the). In addition, if it was me I would make the area a property, not method

So, something like

class Rectangle:
    def __init__(self, height, width):
        self.height = float(height) # allow to take str as input, but raise error when not valid one
        self.width = float(width)

    @property
    def area(self):
        return self.width * self.height

class Square(Rectangle):
    def __init__(self, side):
        super().__init__(side, side)
        self.side = float(side)

    @property
    def side(self):
        return self._side

    @side.setter
    def side(self, value):
        self._side = self._width = self._height = value

    @property
    def height(self):
        return self._height

    @height.setter
    def height(self, value):
        self._side = self._width = self._height = value

    @property
    def width(self):
        return self._width

    @height.setter
    def width(self, value):
        self._side = self._width = self._height = value

if __name__ =='__main__':
    rectangle = Rectangle(3, 4.5)
    print(f'Height: {rectangle.height}')
    print(f'Width: {rectangle.width}')
    print(f'{rectangle.height} X {rectangle.width} rectangle has area of {rectangle.area}')
    square = Square('2')
    print(f'Height: {square.height}')
    print(f'Width: {square.width}')
    print(f'Square with side {square.side} has area of {square.area}')
    square.side = 5.5
    print(f'Height: {square.height}')
    print(f'Width: {square.width}')
    print(f'Square with side {square.side} has area of {square.area}')
    square.area = 5 # this will raise AttributeError: can't set attribute
Output:
Height: 3.0 Width: 4.5 3.0 X 4.5 rectangle has area of 13.5 Height: 2.0 Width: 2.0 Square with side 2.0 has area of 4.0 Height: 5.5 Width: 5.5 Square with side 5.5 has area of 30.25 Height: 6 Width: 6 Square with side 6 has area of 36 Traceback (most recent call last): File "/home/boyan/sandbox2/forum.py", line 95, in <module> square.area = 5 # this will raise AttributeError: can't set attribute AttributeError: can't set attribute
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Calculated surface area - - OOP or functional? Derek Banas Udemy course - by buran - Mar-13-2021, 06:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  functional LEDs in an array or list? // RPi user Doczu 5 1,610 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Stuck in functional proggaming haze hammer 2 1,409 Oct-27-2021, 02:07 PM
Last Post: hammer
  OOP vs functional - - elaborate turn based RPG game (Derek Banas Udemy course again) Drone4four 6 3,942 Mar-14-2021, 08:38 AM
Last Post: ndc85430
  3d Surface where Z is not a function of X and Y richterjan 2 1,735 Nov-11-2020, 04:22 PM
Last Post: michael1789
  Plotting 3D surface plot for non-linear multivariate regression with 5 variables khwajaosama 0 2,712 Jul-02-2020, 04:50 AM
Last Post: khwajaosama
  matplotlib recursion error when repeatedly displaying a surface AdeIsHere 0 1,934 Sep-19-2019, 04:36 PM
Last Post: AdeIsHere
  Learning functional programming vndywarhol 2 2,481 Aug-15-2018, 02:17 AM
Last Post: micseydel
  Projected Surface in 2D [Difficult topic] Hans_K 6 3,863 Aug-02-2017, 09:16 AM
Last Post: Hans_K

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020