OO programming VS. structured programming

Posted on 2006-11-10
Last Modified: 2013-11-23
Hi, I was wondering if someone could give me a brief explanation on the differences between object oriented programming and structured programming? Been searching the net but, I figured this is the best place to get a good explanation.
Question by:savache27

Expert Comment

Comment Utility
what is structured programming? you mean procedural programming?
Object oriented programming is as it sounds all about programming with object. you make an object and you use functions on the objects and you use functions of objects.
procedural programming is, you have variables and functions, and on the variables  you use the functions.
Object Programming has surely more parts like inheration, i. e. an object inherate the properties of the father object.
with procedural you can do it, you can make structures of structures, but strucutres cant use in a secure and controlled way the functions of the other structures.

Accepted Solution

jmar_click earned 300 total points
Comment Utility
Hi savache27,

Noticing the difference between OO and structured/procedural programming can be hard to grasp at first, even harder to appreciate the advantages of OOP. So i'll try to keep it simple. Best way to find out is to start building some classes and you'll understand.

Think of an object/class as a specialized group of functions with the sole task of handling all the needs of something, like a user.

Here is one of my user objects. Notice that the insertd and updates don't have data validation yet. Now I have 2 options, one, validate the data before I call the create() function, or better yet, validate the data within the create function, that way I minimize the code in my main users.php page and leave all processing to the class, thus separating the presentation layer from the data layer.

class user{

      var $userId;
      var $userName;
      var $passWord;
      var $status;
      var $createDate;
      var $userType;
      var $appId; #We'll use this when attaching a user to an application
      var $sortField;
      var $sortDir;

      function create(){
      //First - Create new user            
      $sql="INSERT INTO users (userName, passWord, userType)
      mysql_query($sql) or die("Can't insert new user: " . mysql_error());
      //Second - Return new user id
      return $this->getLastRecord();
      }#End create()
      function read(){
            $sql = "SELECT users.*, user_types.userType FROM users
            INNER JOIN `user_types` ON (`users`.userType = `user_types`.userTypeId)
            WHERE status = 1 ORDER BY $this->sortField $this->sortDir";
            $rs = executeQuery(dopaging($sql,15));
            return $rs;
      }#End read()
      function update(){
            $sql = "UPDATE users set
            userName = '$this->userName',
            passWord = '$this->passWord',
            userType = '$this->userType'            
            WHERE userId = " . $this->userId;

            return "Error updating user: " . mysql_error();
      }#End update()
      function delete(){
            $sql = "UPDATE users SET status = 0 WHERE userId =" . $this->userId;            
            return "Error suspending user: " . mysql_error();
      }#End delete
      function detail(){            
            $sql = "SELECT * FROM users WHERE userId = " . $this->userId;            
            $sqlExe = mysql_query($sql);
            $sqlRs = mysql_fetch_object($sqlExe);
            return $sqlRs;
      }#End detail()
      function getLastRecord(){
            $sql = "SELECT MAX(userId) as lastId FROM users";
            $sqlExe = mysql_query($sql);
            $maxId = mysql_fetch_object($sqlExe);
            return $maxId->lastId;
      }#End getLastRecord()
      function addUser2App(){
            $SQL = "INSERT INTO applicationusers (applicationId, userId)
            mysql_query($SQL) or die("There was an error adding this user to the application list: " . mysql_error());
      }#End addUser2App

}#End class


Take for example a contact form. You would either have all your sql updates, delete, select and insert statements on the same contact.php page. That can lead to bad spaghetti code. Then if you needed to read the list of users somewhere else you'd have to copy and paste the SELECT code again, unless you have it in a function.

The benefit of the user class/object is that when I create a new instance of the user class ($newUser = &New user;) I can assign values to the variables inside the object, and then retrieve them at any given time during the life span of the script. This makes updating, inserting, delete and updting very easy and very clean.

If I wanted to update a users record all i'd have to do is say: (and also remember all my data validation could take place inside the class)

      $userId = $_POST["userId"];
      $newUser->userId = $userId;
      $newUser->userName = $userName;
      $newUser->passWord = $passWord;
      $newUser->userType = $userType;
      header("location: manage_users.php?Action=Edit&Id=$userId&msg=Updated"); exit;

That beats doing the validation in the main page and having SQL statements here too.

I also have  pagination and a column sorting objects which handle sorting and pagination on tables. I never have to manually create pagination for reports, and users love to always have the ebility to sort by clicking column headers. And I love being able to implement those features by just coping and pasting 3 or 4 lines of code.

Here's some good reading to close things off

good luck

Expert Comment

Comment Utility
A couple of really good books - easy, quick reads - that summarize Object Oriented Programming:

The Object-Oriented Thought Process

Object Technology:  A Manager's Guide

The Object-Oriented Thought Process has good Java examples in it to really bring out the concepts.
LVL 16

Expert Comment

Comment Utility
Yeah, to be perfectly clear, the "opposite" of object oriented programming is procedural programming

You should be writing structured code in either case (though modern languages make it hard, or at least inconvenient, to do anything else these days).

Author Comment

Comment Utility
Thanks for the help everyone. I really appreciate it. I knew I could count on the place for some detailed answers.

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 48
mapAB Challlenge 35 84
oracle query help 18 74
Counting documents in a Domino View 3 31
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now