1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 4.25 out of 5)
Loading ... Loading ...

Introduction

OOP stands for Object-Oriented Programming. What does it mean? And what’s the difference between OOP and procedural programming? We’ll go through all these terms, We’ll demonstrate the difference of these approaches and advantages of the OOP over procedural concept. Examples are for PHP, but concept is the same in all object-oriented languages.

So, let’s start with defining, what OOP is and how it differs from procedural programming. Just take a look around. For example, you see a table. Consider you have to program it. When using procedural approach, you focus on it’s behaviour (for example, you write function, that calculates height or that assembles it from the given dimensions). However, table has properties besides behaviour. When following procedural paradigm, we create variables for each property – height, width, length, square, weight. All that data – properties (variables) and behaviour (functions) describe the object from the real world – table. What’s wrong with this approach? Nothing wrong, everything is fine, when we’re dealing with one table.

Procedural programming – the problem

And now imagine if we have several table types – square, triangle and rounded. Consider the following task. We have to calculate square of every table. Let’s try to do this with procedural approach, without any objects. Each table has only one property, that we’re interested in – side for triangle and square and radius for rounded one. Now we’ll create 3 functions, that calculate the square. (see formulas for equilateral triangle and circle)

  1. function calcSquare($side)
  2. {
  3.         return $side*$side;
  4. }
  5.  
  6. function calcTriangle($side)
  7. {
  8.         return $side*$side*0.433;
  9. }
  10.  
  11. function calcCircle($rad)
  12. {
  13.         return M_PI*$rad*$rad;
  14. }

Everything is fine until we have to print list of tables with squares like:

Table name Type Square
table 1 circle 0.95 m2
table 2 triangle 0.78 m2
table 3 square 1 m2

To process it, we need one more parameter in our array – the type of the table. And adding it makes array a 2-dimensional one:

  1. $tables = array(
  2.         array(‘circle’, 0.7),
  3.         array(‘circle’, 0.8),
  4.         array(‘square’, 0.9),
  5.         array(‘triangle’, 1),
  6.         );

And now let’s program processing of these arrays (using the trick with for loop):

  1. echo ‘<table border="1" cellspacing="0" cellpadding="3">’;
  2. for ($i = 0, $s = sizeof($tables); $i < $s; $i++)
  3. {
  4.         switch ($tables[$i][0])//we have to define which type we’re dealing with
  5.                                         //and call correct function for it
  6.         {
  7.                 case ‘circle’:
  8.                         {
  9.                                 $square = caclCircle($tables[$i][1]);
  10.                                 break;
  11.                         }
  12.                 case ‘square’:
  13.                         {
  14.                                 $square = caclSquare($tables[$i][1]);
  15.                                 break;
  16.                         }
  17.                 case ‘triangle’:
  18.                         {
  19.                                 $square = caclTriangle($tables[$i][1]);
  20.                                 break;
  21.                         }
  22.         }
  23.         echo
  24.         <tr>
  25.                 <td>table ‘.($i+1).‘</td>
  26.                 <td>’.$tables[$i][0].‘</td>
  27.                 <td>’.$square.‘ m<sup>2</sup></td>
  28.         </tr>
  29. ;
  30. }
  31. echo ‘</table>’;

Yes! We did it – created all the stuff we had to and it works fine. But take a look at this. In the array we have to specify the table type, then check it and select the corresponding function. And how about dealing with 10 table types? Adding each type requires adding new calculation function and changing the printing one.

OOP – solution

And now we’ll take a look at the object-oriented solution. Maybe you’ve heard of the 3 principles of the OO programming – Inheritance, Encapsulation, Polymorphism. And all of them are used even in this simple example. But before plunging into the OO world, let’s see, what is object.
When developing, by saying “object” we mean some abstraction of the real world object (not always real, it may be an abstraction), that has some attributes (called properties or fields) and behaviour or actions (called methods).
So since object holds both it’s properties and behaviour, we can define 3 classes and use them all. Yes, we mentioned “classes”. Classes are “blueprints” for objects. Class describes, what properties the object will have, what methods will work with his properties and how. And when we need the object of this class to work with, we instantiate it.

Basically, you can imagine class as an empty pot. It has properties:

  • contents
  • volume

And objects of that class are:

  • Big pot with honey (Contents = “honey”, volume=”big”)
  • Little pot with water (Contents = “water”, volume=”little”)
  • Medium-sized empty pot (Contents = “”, volume=”medium”)

You also can imagine the process of instantiation of the pot as the sequence of operations:

  1. Take blueprint (class definition)
  2. Create pot from clay (allocate memory according to the class definition)
  3. Put something into the pot (Initialize properties with some values)

Steps #1 and #2 are done automatically by the programming language compiler (or interpreter), and the third step depends on the:

  • Class definition – you can specify default values for the properties
  • Class constructor – you can define special function, that takes care of attributes initialization. We’ll talk aabout them later.

So, now we have theory, let’s start coding.

  1. class CircleTable
  2. {
  3.         public $radius;//defining property
  4.  
  5.         public function getSquare()//and method
  6.         {
  7.                 return $this->radius*$this->radius*M_PI;
  8.         }
  9. }
  10.  
  11. class SquareTable
  12. {
  13.         public $side;
  14.  
  15.         public function getSquare()
  16.         {
  17.                 return $this->side*$this->side;
  18.         }
  19. }
  20.  
  21. class TriangleTable
  22. {
  23.         public $side;
  24.  
  25.         public function getSquare()
  26.         {
  27.                 return $this->side*$this->side*0.433;
  28.         }
  29. }

In PHP, when we define the property without specifying it’s visibility, it is considered public. That means, that we can create an object and access it’s public property:

  1. $table = new SquareTable();
  2. $table->side = 0.7;

Other variants are private and protected. When we define property as private, it can be accessed only from methods of the same class. This is used for some internal variables. Protected is the same as private, but it can be accessed from the child classes also. Don’t worry if you don’t understand the concept now, we’ll go through this later.
Note: In other languages default visibility may be different! (e.g. C++ properties are defined as private by default)

Refactoring OO solution

Code refactoring is the process of changing a computer program’s internal structure without modifying its external functional behaviour or existing functionality. This is usually done to improve external or internal non-functional properties of the software, such as code readability, simplify code structure, change code to adhere to a given programming paradigm, improve maintainability, improve extensibility, or increase execution performance. (Wikipedia)

Now let’s take a close look at these classes. All of them have one parameter and a common method getSquare(). We can create the parent object – Table and extend these three classes from it. See the UML diagram:
Tables class model
This should be evident, but we’ll explain what’s there. First of all, we have class Table. It is a parent class for all tables. Then we have three derived classes (arrow points from child to the parent class), which override parent’s getSquare() method.
and the code:

  1. class Table
  2. {
  3.         public function getSquare()
  4.         {
  5.                 return 0;
  6.         }
  7. }
  8. class CircleTable extends Table
  9. {
  10.         public $radius;
  11.  
  12.         public function getSquare()
  13.         {
  14.                 return $this->radius*$this->radius*M_PI;//note $this variable. It points to the current object we’re in.
  15.         }
  16. }
  17.  
  18. class SquareTable extends Table
  19. {
  20.         public $side;
  21.  
  22.         public function getSquare()
  23.         {
  24.                 return $this->side*$this->side;
  25.         }
  26. }
  27.  
  28. class TriangleTable extends Table
  29. {
  30.         public $side;
  31.  
  32.         public function getSquare()
  33.         {
  34.                 return $this->side*$this->side*0.433;
  35.         }
  36. }

Make it work!

So, everything is OK now and we can print the same table using defined classes. Unlike previous approach, we’ll have a simple array of objects here:

  1. //creating the objects
  2. //in real application it is done much more smart
  3. //we’ll see how to do it better a bit later
  4. $tables[0] = new CircleTable;
  5. $tables[0]->radius = 0.7;
  6.  
  7. $tables[1] = new SquareTable;
  8. $tables[1]->side = 0.9;
  9.  
  10. $tables[2] = new TriangleTable;
  11. $tables[2]->side = 1;
  12.  
  13. $tables[3] = new CircleTable;
  14. $tables[3]->radius = 1;
  15.  
  16. //showing table
  17. echo ‘<table border="1" cellspacing="0" cellpadding="2">’;
  18. for ($i = 0, $s = sizeof($tables); $i < $s; $i++)
  19. {
  20.         echo
  21.         <tr>
  22.                 <td>table ‘.($i+1).‘</td>
  23.                 <td>’.$tables[$i]->getSquare().‘ m<sup>2</sup></td>
  24.         </tr>
  25. ;
  26. }
  27. echo ‘</table>’;

Note, that we don’t need any function selection, we just call getSquare() for any object. And since all of them have it (that’s why we defined common parent for them), we get correct result depending on the object we’re working with.
One more benefit is extending this code. Remember, when using procedural approach, to add new table type we had to add new function and modify the printing function. Using object-oriented approach, all we have to do is to create one more class. The printing function (which may contain complex logic or be hidden from the end-user) remains unchanged. So with OOP we write less and, at the same time, get much better solutions.

OOP principles explained

This principle, when different actions are performed when calling the same method for different objects, is called Polymorphism. Inheritance is the simplest thing for understanding – we used this to inherit (programmers usually say “extend”) three table classes from the one generic table class.

Finally, encapsulation is about hiding implementation details from the end-used. Let’s take our example; imagine that there are 2 persons. One of them is excellent mathematician and he created that 4 classed (base class – Table and 3 derived (child) classes). Other should show that list. With procedural approach he should take a look at the procedures and write code, that determines the object type and calls correct function. With OOA (object-oriented approach) he doesn’t have to know that. He just calls getSquare() and doesn’t care about anything.

Further improvements

To make these principles more understandable, let’s improve our code and add some data to the objects. Let’s store height of the table and then print volume, that it occupies. Volume=Square*Height. Square is calculated differently, formula for volume is the same. So we add new property to the parent class – $height. And new method – getVolume(). Note, we don’t change any child class at all!
Revised class model

  1. class Table
  2. {
  3.         public $height;
  4.  
  5.         public function getSquare()
  6.         {
  7.                 return 0;
  8.         }
  9.  
  10.         public function getVolume()
  11.         {
  12.                 return $this->height * $this->getSquare();
  13.         }
  14. }

And here is how it works:

  1. $table = new CircleTable;
  2. $table->radius = 0.7;
  3. $table->height = 0.9;
  4. echo ‘Table square = ‘.$table->getSquare().
  5.  
  6. Table volume = ‘.$table->getVolume();

Note, that we didn’t define $height or method getVolume() in CircleTable class, this is inherited from parent class. Don’t you think it’s very convenient? :)

By the way, we used design pattern here, which is called TemplateMethod – we defined method getVolume(), which uses method getSquare(). And getSquare() itself is defined in the derived classes. Read more about this pattern in my future posts :)

Using constructors

Now let’s do some more improvements – we’ll create constructors for the classes. Constructor is special function, that is called when object is created. We’ve already mentioned them. In PHP 5 constructor is defined as a special function, named __construct. It is a regular method, but it can’t return any value. However, it can be public, private or protected. Usually, constructors are private unless some design pattern is used (e.g. Singleton). Here is the code, that uses constructors for smarter object initialization:

  1. class Table
  2. {
  3.         public $height;
  4.  
  5.         public function __construct($h)
  6.         {
  7.                 $this->height = $h;//setting the height
  8.         }
  9.  
  10.         public function getSquare()
  11.         {
  12.                 return 0;
  13.         }
  14.  
  15.         public function getVolume()
  16.         {
  17.                 return $this->height * $this->getSquare();
  18.         }
  19. }
  20. class CircleTable extends Table
  21. {
  22.         public $radius;
  23.  
  24.         public function __construct($r, $h)
  25.         {
  26.                 parent::__construct($h);//calling the constructor of the parent class, passing height there.
  27.                 $this->radius = $r;//setting the radius
  28.         }
  29.  
  30.         public function getSquare()
  31.         {
  32.                 return $this->radius*$this->radius*M_PI;
  33.         }
  34. }
  35.  
  36. class SquareTable extends Table
  37. {
  38.         public $side;
  39.  
  40.         public function __construct($s, $h)
  41.         {
  42.                 parent::__construct($h);//calling the constructor of the parent class, passing height there.
  43.                 $this->side = $s;//setting the side
  44.         }
  45.  
  46.         public function getSquare()
  47.         {
  48.                 return $this->side*$this->side;
  49.         }
  50. }
  51.  
  52. class TriangleTable extends Table
  53. {
  54.         public $side;
  55.  
  56.         public function __construct($s, $h)
  57.         {
  58.                 parent::__construct($h);//calling the constructor of the parent class, passing height there.
  59.                 $this->side = $s;//setting the side
  60.         }
  61.  
  62.         public function getSquare()
  63.         {
  64.                 return $this->side*$this->side*0.433;
  65.         }
  66. }

Here is how to create such classes:

  1.  
  2. $table = new CircleTable(0.7, 0.9);//passing radius and height to the constructor
  3.  

The following usage is the same, it just helps to initialize object attributes in one line of code, encapsulating some preprocessing. For example, we know that height can’t be negative, so we can correct definition of the Table class:

  1. class Table
  2. {
  3.         public $height;
  4.  
  5.         public function __construct($h)
  6.         {
  7.                 if ($h <= 0) $h = 0; //restrict negative values
  8.                 $this->height = $h;//setting the height
  9.         }
  10.  
  11.         public function getSquare()
  12.         {
  13.                 return 0;
  14.         }
  15.  
  16.         public function getVolume()
  17.         {
  18.                 return $this->height * $this->getSquare();
  19.         }
  20. }

So you can see, using constructors is VERY handy.
Note, if you have the properties public (like in this example), you may still use $table->radius to access it.

Attributes and methods access

Finally, let’s take a look at the properties and methods visibility. As we already mentioned, there are three access levels:

  1. Public – accessible both from class itself ($this->property) and from outside ($user->login)
  2. Protected – accessible from the class itself and from it’s derived classes ($this->property, property may be defined in current class or in the parent one)
  3. Private – accessible only from the class itself ($this->property, property should be defined in the current class)

To demonstrate this in action, we’ll modify the Table class and make it’s $height property private. Since it is used only in getVolume() method, which is also defined in the Table class, it should work fine. Also, we add a new method setHeight(). This will be used to modify height since we’ll not have access to it through $table->height. So here is UML and the code:
Final Table class

  1. class Table
  2. {
  3.         private $height;
  4.  
  5.         public function __construct($h)
  6.         {
  7.                 $this->setHeight($h);
  8.         }
  9.  
  10.         public function setHeight($h)
  11.         {
  12.                 if ($h <= 0) $h = 0; //restrict negative values
  13.                 $this->height = $h;//setting the height
  14.         }
  15.  
  16.         public function getSquare()
  17.         {
  18.                 return 0;
  19.         }
  20.  
  21.         public function getVolume()
  22.         {
  23.                 return $this->height * $this->getSquare();
  24.         }
  25. }

Note, that we changed constructor, not it doesn’t set the height itself, it delegates this to the special setter function, which perform all validation. This is a good practice – to have attributes of an object hidden and have getter/setter methods to access it. Why? Because this way you control, what is returned and what is set and can ensure that object’s properties are always correct.
When using this definition, we’ll not be able to access $height in CircleTable or TriangleTable. To enable this, we can either add getter method or make $height protected. First variant will allow everybody to get the height. Second variant allows to get the $height only within the derived classes (see protected access level above)

Glossary

  • Class is a formal definition of some real object or it’s abstraction. It contains definition of it’s both properties and methods.
  • Object is an instantiated class
  • Property is a special variable, that represents some attribute of an object. Sometimes it is called “member variable”
  • Method is a special function, that describes some behaviour of an object.
  • Constructor is a special method, that is called when object is created. This is mainly used for the object initialization.
  • Getter is a special method, that returns property value. Usually used for private or protected properties or for some kind of processing before returning.
  • Setter is a special method, that sets property value. Used for private or protected variables or for some additional validation.
  • Superclass is same as parent class or base class
  • Subclass is same as derived class, child class, extended class
  • Inheritance is an OOP principle, which is used to define some common behaviour in the base class and then extend it and use in the derived classes.
  • Encapsulation is an OOP principle, which used for hiding implementation details from the end-user. (remember example with two programmers developing these table classes)
  • Polymorphysm in an OOP principle, which allows different classes to behave differently when calling the same method.
  • Design pattern is some standard proven solution to the common problem. For example, Singleton pattern solves the problem when we need some common resource in different places of an application. For example DB abstraction object.
  • Refactoring is the process of changing the source code to make it work faster, look better, use better solutions, be better in support and future development

Further reading

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Share this post with a friend Share this post with a friend

33 Comments

  1. Juan says:

    Very nice article!

    I found it on wikipedia looking for the OOP principles and implementation details and has helped me a lot.

    Thank you very much!

  2. Konstantin Mirin says:

    Thanks! I did my bet to clarify things that were complex for me when I started learning OOP. I’m planning a series of OOP articles, so welcome back in a week or so ;)

  3. John West says:

    Thank you for the OOP explanation. I am 42 years old and have never learned programming. Your explanation has helped me to understand better. I found your page via a link from a wikipedia article on OOP. Thank you for the education.

  4. Konstantin Mirin says:

    Thanks for the feedback! It’s very nice to see positive comments :)
    I’ll definitely keep it going, I like when my info really helps people.

  5. Romanoza says:

    so, in russian, please :) )
    tired of translating :)

  6. Konstantin Mirin says:

    Yes, I’m going to do Russian version also, but I’m short of time last days, not sure when I’ll do this.

  7. Pat says:

    Just to pick on you for a bit:

    “Suberclass is same as parent class or base class” should be “superclass” not “suberclass”

    “Polymorphysm in an OOP principle” should be “is” not “in”

  8. Konstantin Mirin says:

    Thanks! Corrected this.

  9. 3.grosz says:

    This code doesn’t work: Fatal error: Cannot use object of type CircleTable as array in /home/user/public_html/Function/file.php on line 60
    I don’t know how to correct this :(
    please help me
    radius*$this->radius*M_PI;//note $this variable. It points to the current object we’re in.
    }
    }

    class SquareTable extends Table
    {
    public $side;

    public function getSquare()
    {
    return $this->side*$this->side;
    }
    }

    class TriangleTable extends Table
    {
    public $side;

    public function getSquare()
    {
    return $this->side*$this->side*0.433;
    }
    }
    //creating the objects
    //in real application it is done much more smart
    //we’ll see how to do it better a bit later
    $tables[0] = new CircleTable;
    $tables[0]->radius = 0.7;

    $tables[1] = new SquareTable;
    $tables[1]->side = 0.9;

    $tables[2] = new TriangleTable;
    $tables[2]->side = 1;

    $tables[0] = new CircleTable;
    $tables[0]->radius = 1;

    //showing table
    echo ”;
    for ($i = 0, $s = sizeof($tables); $i < $s; $i++)
    {
    echo ‘

    table ‘.($i+1).’
    ‘.$tables[$i][0].’
    ‘.$tables[$i]->getSquare().’ m2

    ‘;
    }
    echo ”;
    ?>

  10. 3.grosz says:

    Something changed this code when I had submitted comment. So if I take yours code, PHP shows me Fatal error: Cannot use object of type CircleTable as array in /home/user/public_html/Function/file.php on line 60. I don’t know how to correct this :(
    please help me

  11. Konstantin Mirin says:

    Hi, thanks for spotting that! The problem was in my code, I didn’t remove the line from the functional-oriented example asn was trying to get the type of table using $tables[$i][0]. But in the $tables[$i] we don’t have an array, we have an object there. In my example, we don’t store type of an object in some property (we can introduce one in the Table class (public $type) and populate it when creating objects. Bu we don’t have it, so in object-oriented example we just have 2-columns table.
    I tested the example, corrected it and it’s fine now. Sorry for messing you up!

  12. Thành Đạt says:

    Thanks, very helpful. I’m becoming your fan :D

  13. Konstantin Mirin says:

    Thanks, I tried to do it as helpful as possible!

  14. Sahay says:

    Nice article.
    Thanks.

  15. Konstantin Mirin says:

    thanks for the comment!

  16. Will says:

    I was a fortran, cobol programmer–I still don’t get it. Lots of new words but not lots of clarity????

    Will

  17. Konstantin Mirin says:

    Will, thank you for the comment!
    I think, you didn’t read it attentively because I have mostly positive feedback here. What didn’t you understand in this post?

  18. scrutinizr says:

    <?
    /* a simple procedure dispatcher is all you need. no objects/classes required! just try it. it shows you the desired table!
    */

    function calcSquare($side)
    {
    return $side*$side;
    }

    function calcTriangle($side)
    {
    return $side*$side*0.433;
    }

    function calcCircle($rad)
    {
    return M_PI*$rad*$rad;

    }

    // init data
    $tables = array(
    array(‘calcCircle’, 0.7),
    array(‘calcCircle’, 0.8),
    array(‘calcSquare’, 0.9),
    array(‘calcTriangle’, 1),
    );

    //showing table
    echo ”;
    for ($i = 0, $s = sizeof($tables); $i < $s; $i++)
    {
    echo ‘
    table ‘.($i+1).’
    ‘.$tables[$i][0].’
    ‘.$tables[$i][0]($tables[$i][1]).’ m2
    ‘;
    }
    echo ”;
    ?>

  19. scrutinizr says:

    here’s the programm output:

    table 1
    calcCircle
    1.539380400259 m2

    table 2
    calcCircle

    2.0106192982975 m2

    table 3
    calcSquare
    0.81 m2

    table 4
    calcTriangle
    0.433 m2

  20. Pascal says:

    Great article, thanks!

    I think there are a few typos in the Glossary:
    suPerclass is same as parent class or base class
    subclAss is same as derived class, child class, extended class
    inheritance is AN oop principle, which is used to define some common behaviour in the base class and then extend it and use in the derived classes.

  21. Konstantin Mirin says:

    @scrutinizr
    Yes, it is possible to handle this with dispatching function for this particular task. But how about extending some functionality? Yes, I know that polymorphism may be emulated with dispatching function. Encapsulation – with local variables and functions (but that is a really ugly solution). And there is no way to emulate inheritance. By the way, it is a great thing about OOP :)
    The aim of the article was not to show the best way of printing tables, but rather to show how it can be done with OO principles and illustrate the process. I think this was done quite well :)

  22. Konstantin Mirin says:

    @Pascal
    Thank you for the comment. I’ve corrected everything :)

  23. Tom Sedlak says:

    Very good explanation with perfect examples. The best way how to understand abstractions and theory. Thank you.

  24. scrutinizr says:

    @Konstantin

    > Encapsulation – with local variables and functions (but that is a really ugly solution).

    conceptually, for encapsulation you just need a module concept of which simplest and best solution provides Oberon. see http://www.inf.ethz.ch/personal/wirth/Articles/Oberon/Oberon07.Report.pdf
    as linked from http://www.inf.ethz.ch/personal/wirth/Articles/Oberon.html

    > And there is no way to emulate inheritance.

    it is not necessary to provide inheritance as programming language concept. see — you guessed it.

    > By the way, it is a great thing about OOP :)

    so many things are so great that this word has empty meaning except that you shoot yourself in the foot. ;)

    > The aim of the article was not to show the best way of printing tables, but rather to show how it can be done with OO principles and illustrate the process.

    we need suitable examples to show superiority of any concept. PHP (pigest hack plus); the acronym says it all. :)

    See why OOP has failed:

    http://www.4js.com/en/fichiers/b_genero/pourquoi/Has_OOP_Failed_Sept_2005.pdf

    > I think this was done quite well :)

    well heavyonthewoodway :)

  25. Konstantin Mirin says:

    @scrutinizr
    1) I realize that OOP is not the only concept in the development world. I read the article you linked to (Has OOP Failed?). It’s an interesting point of view, but it is too emotional. Lots of words, very few examples. Sometimes comparison between OOP and procedural style are inadequate. For example, the printing example (page 6). The author missed the following things:
    – 80-line OOP code allows much more flexibility.
    – 80-line OOP code may be wrapped into class and then called in the form of myPrint->setFontSize(11); myPrint->print(text); Right the same may be done with the driver calls in the procedural style.
    Comparison the code for the colour changing and font changing (page 7) reveals the poor design of the VB.NET API, this sort of inconsistency is not caused by OOP itself.
    The author points to the lack of works comparing OOP and procedural styles and their efficiency. This is absolutely true just because such comparison is almost impossible. You can’t compare “old-style basic” to the VB.NET because the latter provides much more features, allows more control etc.
    The excellent experiment is to develop some real-world application using the pure OO and pure procedural approach and compare results – performance, maintainability, code readability etc. By the way, PHP allows to do this since not “everything is an object” here, unlike Java, Python etc.
    2) I can’t but agree that OOP isn’t the best choice for algorithm implementation. Sometimes it is, but OOP is more about classification, structure handling than logic implementation.
    3) I entirely disagree with the author that future paradigm should be procedural. I think, that combination of these things would do much better. You can’t say OOP does bad with UI, right? So why code it in procedural style if OO does the same easier? Nice combination makes it all.

    Finally, the blog post was aimed to show what is OOP, not “why it is better than functions”. Yes, I had some words about that, but it is not the main line. These comments serve mainly for the post discussion, so discussing “OOP vs procedures” is basically flooding here. I’d like to continue this discussion by email or any IM.

    Anyway, thank you for the comments! Though we have opposite thoughts, my post led to this interesting discussion and I like it :) Thank you for reading my blog!

  26. scrutinizr says:

    hello,

    in summary, what I wanted to show was:

    1. for the example of this thread, OOP is not necessary.
    as shown, it can be done simpler by using procedure variables.

    2. OOP, if needed for programm development, is not bad per se, but the OOP features provided any particular programming language must be minimal and sufficient to provide for any OO model.

    This is why I linked to Oberon wich stands out as the simplest (and yet powerful) OOP language. (A complete operating system, compiler, hundreds of non-trivial applications have been written in Oberon – free and open source! – yet it didn’t gain acceptance by industry. There’s also an Oberon compiler for ARM (embedded real-time programming written by legendary professor Niklaus Wirth
    and father of Pascal and Modula-2 himself. just follow the link I gave above.)

    Oberon provides for the main OOP concept namely extensible programming; its implemenations feature pointer safety and garbage collector.
    Look at the language report. it has only 20 pages – including examples!

    The retired master (still privately active in hardware (verilog – FGPA*) and software development –> Oberon-07 for ARM processor says, industry – using ever more complex programming languages and libraries -
    made no progress in software development since the early 1970s!

    You might be interested in listening to Niklaus Wirth’s opening talk at GTAC 2009 (Google Test Automation Conference) about programming language development
    history and in relation to software testing:
    http://www.youtube.com/watch?v=8W5Jd_wzB90

    happy Christmas!

    *) want to learn programming FPGAs?
    see http://www.fpga4fun.com/PWM_DAC.html
    they have nice development (FPGA/ARM) boards: http://www.knjn.com/FPGA-FX2.html

  27. scrutinizr says:

    p.s. you can contact me via the specified email although I think we agree in principle.

    p.s.s.: There’s now also a free open source Oberon-to-javascript compiler for web programming. It’s written in JS and has only 1000 source lines. It is called “Oberon Script” but it is the (full) Oberon language and it compiles on the fly into JS when the browser opens the page. Works with any browser. See http://research.microsoft.com/pubs/70288/tr-2006-50.pdf Ralph Sommerer: Oberon Script: A Lightweight Compiler and Runtime System for the Web
    http://research.microsoft.com/research/pubs/view.aspx?type=technical+report&id=1094
    I don’t know the project status. Source code is here: http://www.ralphsommerer.com/obn.htm

  28. scrutinizr says:

    Here you can try out an Oberon Script example:
    http://www.modulaware.com/test/fibo-test-eth.htm

    just click the link, view/source code and you see the Oberon test programm source (Fibonacci number calculation). click the “Test”-button to see the result. view http://www.modulaware.com/test/oberon.js to see complete source code of Oberon-script compiler and run-time system.

  29. Alistair Nel says:

    To be able to teach others is a gift. Thanks Konstantin. You’re a legend ;)

  30. McFry says:

    Thnx for this great article! Keep it up!

  31. Low Coupling and High Cohesion – GRASP (Design patterns series) | Programmer's Notes says:

    [...] you should print the table parameters (see What is OOP post for this example). There are tables of 2 types: square and circle ones. Square has parameter [...]

  32. Brian says:

    Excellent review. Quick read (5 minutes) and good reminder why old farts who hate commenting and object oriented programming and love writing everything in one unreadable line are wrong when they say the only advantage to object oriented programming is damage control of mediocre programmers. Maybe they work in embedded systems all the time who knows but for the rest of us who have to model real life, object oriented programming is fundamental.

  33. Konstantin Mirin says:

    Thanks! Yes, I think so too that’s why I’m using OOP almost everywhere :)

Leave a Reply