Is Procedural looked down upon compared to OOP?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Is Procedural looked down upon compared to OOP?

Post by swraman »

Hi,

This may sound dumb, but now that I am writing scripts for other people I want to know:

Is OOP suposed to be "classier" or cleaner than regular old procedural programming? Is is suposed to be better to write PHP with objects?

up till now I have only been doing procedural programming, functions seem to do everything I need them to do. But would the code "look better" in the professional's eye if it was done with object oriented programming instead?

Thanks
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Is Procedural looked down upon compared to OOP?

Post by Benjamin »

The best way to put this, from my perspective, is that the functions really aren't doing everything you need them to do, you just think they are.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Is Procedural looked down upon compared to OOP?

Post by Theory? »

I love the warring factions mindset. Balance is the key to true zen.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is Procedural looked down upon compared to OOP?

Post by Christopher »

swraman wrote:Is OOP suposed to be "classier" or cleaner than regular old procedural programming? Is is suposed to be better to write PHP with objects?

up till now I have only been doing procedural programming, functions seem to do everything I need them to do. But would the code "look better" in the professional's eye if it was done with object oriented programming instead?
I think your attitude shows part of the problem with discussing methodologies. Do you want do look good or do you want to produce well designed and maintainable applications.

And I want to be clear that in PHP you still program procedurally when you are programming using OOP. Vice versa that is not the case.

If you are not using OOP then you are simply choosing to not use some powerful language constructs that exist in PHP. So if you are choosing not to use certain parts of the language (in this case 'class' but it could be 'switch', 'foreach', etc.) then you have to ask yourself why you are choosing not to use them. Do they lead to bad practices like 'goto' for example? Do you not understand how to use them?

There is nothing inherently good or bad about any of these language constructs. Each is useful in certain circumstances.
(#10850)
FunkyDude
Forum Newbie
Posts: 8
Joined: Mon Dec 15, 2008 5:19 pm

Re: Is Procedural looked down upon compared to OOP?

Post by FunkyDude »

I've been reading/learning OOP, and from what I'm understanding, its more about organization, or making your code easier to reuse, extend, or share with others for them to improve on. Writing a script in OOP or procedurally can give the same end result.
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Is Procedural looked down upon compared to OOP?

Post by swraman »

arborint wrote:
swraman wrote:Is OOP suposed to be "classier" or cleaner than regular old procedural programming? Is is suposed to be better to write PHP with objects?

up till now I have only been doing procedural programming, functions seem to do everything I need them to do. But would the code "look better" in the professional's eye if it was done with object oriented programming instead?
I think your attitude shows part of the problem with discussing methodologies. Do you want do look good or do you want to produce well designed and maintainable applications.

And I want to be clear that in PHP you still program procedurally when you are programming using OOP. Vice versa that is not the case.

If you are not using OOP then you are simply choosing to not use some powerful language constructs that exist in PHP. So if you are choosing not to use certain parts of the language (in this case 'class' but it could be 'switch', 'foreach', etc.) then you have to ask yourself why you are choosing not to use them. Do they lead to bad practices like 'goto' for example? Do you not understand how to use them?

There is nothing inherently good or bad about any of these language constructs. Each is useful in certain circumstances.
I understand how to use them, but I've never actually programmed anything using OOP techniques. But now on a project im working on I am noticing a lot of places that I can use objects to replace the code I currently have, and it would probably make the code look a bit cleaner. Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Is Procedural looked down upon compared to OOP?

Post by swraman »

astions wrote:The best way to put this, from my perspective, is that the functions really aren't doing everything you need them to do, you just think they are.
I believe you, but I honestly dont know where OOP can do something that procedural can't...

Is there any way to have an input arguement to a class?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Is Procedural looked down upon compared to OOP?

Post by Benjamin »

Here is a simple example of a class that represents an object:

Code: Select all

 
class number {
    private $_n;
 
    public function __construct($n = 0, $base = 10) {
        $this->_store($n, $base);
    }
 
    public function set($n, $base = 10) {
        $this->_store($n, $base);
    }
 
    public function get($base = 10) {
        return $this->_retrieve($base);
    }
 
    private function _store($n, $base) {
        switch ($base) {
            case 2:
                $this->_n = bindec($n);
                break;
            case 8:
                $this->_n = octdec($n);
                break;
            case 10:
                $this->_n = $n;
                break;
            case 16:
                $this->_n = dexhec($n);
                break;
            default:
                $this->_n = $n;
                trigger_error("" . get_class($this) . "() Second parameter must be one of 2, 8, 10, 16");
                break;
        }
    }
 
    private function _retrieve($base) {
        switch ($base) {
            case 2:
                 return decbin($this->_n);
                break;
            case 8:
                return decoct($this->_n);
                break;
            case 10:
                return $this->_n;
                break;
            case 16:
                return hexdec($this->_n);
                break;
            default:
                trigger_error("" . get_class($this) . "() Second parameter must be one of 2, 8, 10, 16");
                return $this->_n;
                break;
        }
    }
}
 
And it's usage:

Code: Select all

 
$n = new number(50);
echo $n->get(2) . "<br />";
echo $n->get(8) . "<br />";
echo $n->get(10) . "<br />";
echo $n->get(16) . "<br />";
 
Now, you can pass this object to other objects that can retrieve it's value in the format they need it in, without having to worry about doing it yourself.

This would allow you to create a calculator class that could perform math on a number regardless of it's base. This is just an example though, it would only actually work on real numbers.
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: Is Procedural looked down upon compared to OOP?

Post by VirtuosiMedia »

I wrote a fairly lengthy post on another forum about OOP vs. Procedural programming. You might find it useful.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Is Procedural looked down upon compared to OOP?

Post by allspiritseve »

swraman wrote:I understand how to use them, but I've never actually programmed anything using OOP techniques. But now on a project im working on I am noticing a lot of places that I can use objects to replace the code I currently have, and it would probably make the code look a bit cleaner. Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
Simply "converting the code... into code that uses OOP" doesn't mean you're programming in an object-oriented manner. In fact, most likely you are still coding procedurally, and will end up using classes as a pseudo-namespace for functions. The way I like to describe it, is programming OOP is a different mindset than programming OOP, and you really have to think differently in order to truly benefit from those "powerful language constructs". So convert away, but realize there's a lot more to OOP than just "clean code"
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is Procedural looked down upon compared to OOP?

Post by Christopher »

swraman wrote:Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
I would not convert existing working code. But as you create new code, or refactor existing code, look for opportunities to use OO design and OOP. Keep at it.
(#10850)
Post Reply