#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12

    OOP(Don't get why?)


    Okay I have been wondering why is OOP so famous (and good supposely)

    Why is OOP used so much and why is it considered good.

    I can code stuff like it and also organized without classes and things...

    Why?

    And I have a coding problem...

    How do delete a line in a txt file
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    OOP is a very popular and will be popular for a very long time from now, forum of programing. Almost every modern programing language is oo, if any new commers want to be even thought about they most be capible of oo.

    The reasons oo is great is because it simplifies things and treats everything in a diff manner than normal programing does. With oo you can reuse code very easly. When i say reuse i mean within the same program and with other projects/programs. I know you probably know all about functions and such well Imagin that you can group a bunch of functions together that relie on eachother and work with eachother. Well for that to work you have to call the other sending all the premiters, and such. Then the other function has to know witch premiters are being sent and it can turn into a mess.

    with OO it isn't like that. You set up a logical grouping of functions and veribles. Then you make the hole class in its hole. Then its done. no going back and switching things around. Then you go on to the next class make the hole thing then its done. Set up one single way for one class to talk to the other and its all work. No going back and testing this if statment to see if it will not cancel out the others. For premitors within the class you can just need self. witch will have all the curren't state of the veribles that are shared between everything. So you could have 3 things of the same class running all diff.

    You could i suppose to everything with conventional ways of programing that you can do with oo, but that would be absolutly stupid. You can also make the same program in convetional maners that you could in OO but the OO would be supior because lets say you want to add more to the conventional app, you will spend at least 4 times as much time trying to make it work, possible rewriting half the application again. While with OO you just had to edit a few lines and add a few new methods, and your done.

    Trust me, i use to be like you. I didn't understand why OO was so important, i actually thought it was dumb. It only took me to learn Java, a lang that forces you to use OO, for me to actually learn the importance of it. Once you use OO you will never want to code in any other manner.

    Comments on this post

    • Yegg` agrees
    • SimonGreenhill agrees
    • jacktasia agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Mostly agree, but:
    Once you use OO you will never want to code in any other manner.
    OO isn't the be all and end all of programming styles - there's also logic programming (Prolog), functional programming (Haskell, ML, etc), aspect oriented programming (AspectJ)... and sometimes plain procedural programming is all that's required, particularly for short scripts.

    Just as procedural programming allows code to be simplified through factoring out commonalities of code, OO allows code to be simplified through factoring out commonalities of interface and organised through the collection of behaviours for notional "things" eg:
    Code:
    import math
    
    def calculate_square_area(side):
        return side**2
    
    def calculate_rectangle_area(length, width):
        return length*width
    
    def calculate_circle_area(radius):
        return math.pi*radius**2
    
    rectangles = [(5,15),
                  (10,12)]
    for rect in rectangles:
        print calculate_rectangle_area(*rect)
    Fairly simple... and certainly a lot shorter than the OO version:
    Code:
    import math
    
    class Shape(object):
        def calculate_area(self):
            raise NotImplementedError
    
    class Square(Shape):
        def __init__(self, side):
            self.side = side
    
        def calculate_area(self):
            return self.side**2
    
    class Rectangle(Shape):
        def __init__(self, length, width):
            self.length = length
            self.width = width
    
        def calculate_area(self):
            return self.length*self.width
    
    class Circle(Shape):
        def __init__(self, radius):
            self.radius = radius
    
        def calculate_area(self):
            return math.pi*self.radius**2
    
    rectangles = [Rectangle(5,15),
                  Rectangle(10,12)]
    for rect in rectangles:
        print rect.calculate_area()
    Ok ... so it looks like all OO gives you is a lot of extra typing, right?

    But what if you want to have a list of different shapes - you then have to associate a type with data - the procedural version would be:
    Code:
    shapes = [("CIRCLE", (7)), 
              ("RECTANGLE", (10,12)),
              ("SQUARE", (4))]
    
    area_lookup = ["CIRCLE": calculate_circle_area,
                   "RECTANGLE": calculate_rectangle_area,
                   "SQUARE": calculate_square_area]
    
    for shape in shapes:
        shape_type, dimensions = shape
        area_func = area_lookup[shape_type]
        print area_func(*rect)
    ... no longer quite so simple compared with:
    Code:
    shapes = [Circle(7), 
              Rectangle(10,12),
              Square(4)]
    
    for shape in shapes:
        print shape.calculate_area()
    ... and what if you wanted to add in colours and patterns to your shape ... and a function to calculate the perimeter... and more shapes. And what if you wanted to then add a coordinate system... and overlap shapes to create new shapes. Suddenly using functions no longer seems so simple.

    OO allows you to simplify bookkeeping as your code grows by making you do a little more up-front work.

    --OH.
    Last edited by hydroxide; April 17th, 2005 at 03:44 AM. Reason: Fix bug in Circle.calculate_area() [*blush*]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Okay I get the point now

    But whats with deleting a line in a txt file...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by monkeyman23555
    Okay I get the point now

    But whats with deleting a line in a txt file...
    Which line?

    the simplest way to delete a line in a file is to copy the file line by line to another file except the line you want to delete. When you are done, delete the old file and rename the new file to it. Depending on how you identify the line you want to delete, you could do it with unix tools such as grep or sed. If you are on a Windows machine then download cygwin and use a unix tool such as grep or sed.

    This may take a while if the file is a billion lines long.

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Thanks

    Max lines are about 2000
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Unhappy


    Hydroxide,
    your OO Circle doesn't work even after fixing the formula. I get this error ...
    Code:
    ...
    calculate_area
        return math.pi*radius**2
    NameError: global name 'radius' is not defined
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Put a self. infront of the radius
    It is defined as self.radius = radius
    Meaning not radius but self.radius

    Edit: This is my 100th post
    Another 100
    Last edited by monkeyman23555; April 17th, 2005 at 02:14 PM.
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Thanks, looks like Hydroxide corrected it already.

IMN logo majestic logo threadwatch logo seochat tools logo