Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

ID: 17918544
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
ID: 17918890
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

ID: 17919226
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

ID: 17919310
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

ID: 17919881
Thanks for the help everyone. I really appreciate it. I knew I could count on the place for some detailed answers.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is an explanation of a simple data model to help parse a JSON feed
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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