-3

Would someone please explain this to me? Everything that I've read and heard about OOP makes it sound like procedural programming. Help?

Amit Sharma
  • 5,844
  • 5
  • 25
  • 34
kookman98
  • 259
  • 1
  • 4
  • 13
  • 3
    How would you explain normal programming? – Chris Leyva Dec 20 '13 at 21:15
  • 1
    Do you mean Procedural programming? – user1336827 Dec 20 '13 at 21:16
  • "normal programming" is known as _procedural_ programming, where you feed data to routines. OOP would be using data with defined _methods_ (routines) operating on the data. That's the 50-thousand foot view. – Phil Perry Dec 20 '13 at 21:18
  • For a large chunk of devs, OOP IS normal programming. – BBlake Dec 20 '13 at 21:18
  • You should let us know what specific things you have heard about oop and normal programming that is creating the confusion. That way we will have something concrete to address here. – Mark Bolusmjak Dec 20 '13 at 21:20
  • I'm curious what makes the two users above associate "normal programming" with procedural programming. It's not like it's the first or most fundamental paradigm. It's not terribly widespread (any more) either, in many many environments. –  Dec 20 '13 at 21:22
  • @delnan probably simply because its the most common "not OOP" practice, while taking the assumption that OP's "normal" programming is not mistakenly OOP programming in disguise. – DoubleDouble Dec 20 '13 at 21:37

2 Answers2

4

I'm going to take a shot in the dark and assume you're either (a) Picking up coding for the first time because you're taking some class or (b) Picking up coding for the first time because you feel like it. Either way, good choice, it's pretty fun

Either way, you're new to it, and you're probably writing bits of code from a type of exercise book (whether it's online or in a classroom). Code that probably looks like this.

Assignment: populate a list with all prime numbers between 1 and 100

def makePrimeList():
    my_list = []
    for x in range(1, 100):
        if isPrime(x):
            my_list.append(x)
    return my_list

def isPrime(x):
    if x<2:
        return False
    for i in range(2,x):
        if not x%i:
           return False
    return True 

This kind of programming is extremely useful to learn how to code. it teaches you what code can do on a VERY basic level. However, when you get to do more complex things, your going to have to use OOP, and make some classes.

The basic premise of OOP is that you create some classes and then do stuff with those classes. A class is basically a way of creating your own object (hence the "Object" in Object-Oriented Programming). You could, for example, make a Card class. This class would probably have some attributes such as Card.number or Card.suit which would describe the Card. It would look something like this in Python:

Class Card:
    def __init__(self, number, suit):  #This method, called an __init__ method, creates a card
        self.number=number
        self.suit=suit

Now, I could call the code my_new_card = Card(4, "spades") to return a card. If I then said in my code my_new_card.number, it would return 4 and my_new_card.suit would return "spades". So, as you can see, there are a bunch of ways that OOP can change the way you think about, and use code. Some more info on Classes can be found on the wiki page Classes. The way I think about it: a class is something very much like a list or a dict in that you decide how it is formatted and what functions you can tag onto it. Just like, the list has the .append() method, you could tag this onto the Card class:

def doubleUp(card):
    card.number *= 2

All this method does is double the number of the card you inputted into the function. Nothing special, but the concept goes a long way.

Anyway, this is a super broad overview, none of the above code is tested, but it's just there to give you a sense of the massive scope your question is asking. Check out this guide for some OOP knowledge.

2

There is a number of articles explaining difference between OOP and other programming paradigms.

E.g.: http://saimaterial.wordpress.com/2007/09/14/1what-is-the-difference-between-object-oriented-programming-and-procedural-programming/

OOP vs Functional Programming vs Procedural

Try to search web for "difference between oop and procedural programming", "difference between oop and functional programming" etc...

Community
  • 1
  • 1
Yuri Solodkin
  • 530
  • 8
  • 26