DevPinoy.org
A Filipino Developers Community
   
Procedural Object-Oriented Programming (POOP)

At some point in one's career, a typical developer would encounter the procedural vs. object-oriented programming (OOP) debates. For instance, this page explains why it is preferable to do procedural programming over OOP, while this page tells of the so-called benefits of object-oriented programming over procedural. So, which one do you believe? Or which one do you prefer?

It is not a surprise that there are developers who claim to know OOP yet code in a procedural manner. I came from a company wherein the top tech man espoused the practice of having few classes each containing lots of methods...and static methods at that! Just because the code is in C# doesn't mean it's object-oriented. Most of today's development platforms (Java, .NET, PHP, Ruby, etc...) are OO-capable, yet how come it seems not many really know how to think and design the object-oriented way?

I put the "fault" here on programming languages like C++ which treat class methods as if they're just glorified functions (or procedures, in VB- or Pascal-speak). In my opinion, by making class methods appear just like regular procedural functions and putting forth the oft-repeated mantra that objects = code + data, they made it easy for programmers used to procedural programming to slide into an object-oriented language without really understanding the OO paradigm. I also partially put the blame on learning institutions that teach an OO-capable language (like Java) as the introductory programming language for their students.

I'll take myself as an example. I've "known" OOP for about fifteen years already, my curiosity piqued after reading full-page ads about them (oh, how magnificent Borland was back then!) and the "objects = code + data" thing. Yet it was only in the recent years of my software development career that I could claim that I truly knew what it was to be object-oriented. Why is this so? Because I thought that all it takes to be object-oriented was to have code and data in the same place. I thought back then that classes were just glorified structs (having learned C first, then C++). I thought of OOP in procedural terms, and it showed in my code and designs for many, many years.

What I believe new programmers should learn is that OOP is all about objects sending messages to each other. Yes, sending messages, not accessing properties and fields and calling methods. If I only think of it as accessing member fields/properties and calling methods which look just like ordinary procedures/functions, I fall into a trap and tend to think in a procedural manner -- the only difference being my data structures located inside the object/class itself instead of having a separate struct. This article about the design principles behind Smalltalk is a must read. It helped open my perspective about what objects should really do and how they should collaborate. Only then did design patterns begin to really make sense to me. Programming principles such as "Tell, Don't Ask" are a must-learn for those interested in OOP. And hopefully as an industry we can stop churning out POOP but truly OOP designs.

UPDATED 22 January 2010: Changed the "Design Principles Behind Smalltalk" link, as the previous URL no longer exists. Thanks to Brian Caufield for pointing it out!


Posted 07-10-2007 5:59 PM by cruizer

Comments

lamia wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 4:40 AM

[quote]

I came from a company wherein the top tech man espoused the practice of having few classes each containing lots of methods...and static methods at that!

[/quote]

To share my experience in this. I use static methods whenever I create "Helper" classes or "Utility" classes. But to use them with your business objects... YUCK!

[quote]

I thought back then that classes were just glorified structs (having learned C first, then C++)

[/quote]

In defense for the C/C++ programmers. I try to minimize the use of classes when I make games. I am guilty of being tempted(and having done so) to break encapsulation that instead of having private fields in your for example, int x member I just make them public(as opposed to making accessor, mutator methods for them). This is of course, all in the name of efficiency.

I'd like to admit though that back then, classes were just glorified structs. :)

[quote]

also partially put the blame on learning institutions that teach an OO-capable language (like Java) as the introductory programming language for their students.

[/quote]

Teachers and students should be handed a Head First Java book for this to be solved! Lolz!

[quote]

What I believe new programmers should learn is that OOP is all about objects sending messages to each other.

[/quote]

Isn't this smalltalk? Yeah I've read this somewhere. I'll take a peak at your given links.

adonhis wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 3:55 PM

"Adding objects to your code is like adding salt to a dish: use a little, and it's a savory seasoning; add too much and it utterly ruins the meal." --> www.codinghorror.com/.../000801.html

For me, coding in Procedural Object-Oriented Programming (POOP) is just okay. It's a learning path and process in improving your code to a pure OOP manner. And using a framework like .Net, Ruby on Rails and alike will help you to attain it. :)

willydavidjr wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 4:44 PM

As I noticed, OOP is a big concept and you cannot really learn it just like sir cruizer have told us. Sir what books do you prefer to read so that I can fully and easily understand OOP? It seems that I am not aware of objects sending messages to each other. What I am implementing is only accessing properties and fields.

cruizer wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 7:01 PM

willy, you are right. for many of us we just think of OOP as accessing a class' members. but you need to realize that OOP is about sending messages to members. when you call a class' method, you are actually sending a message to it. when you retrieve the value of an object's property you are sending it a message to inform you about something.

when you start to think of objects that way, you will start to design code in such a way that your logic is well-distributed among objects, instead of the usual single train of processing where your main object (probably your winform or your asp.net code behind!) does everything by itself.

n.ocampo wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 7:46 PM

Learning OOP is not easy as 1-2-3. It takes time to develop that skills, it is a continous learning. I myself, have struggling with these. And guilty with POOP :-( . I have never had a chance to work with some of the bright people that could somehow help me to develop my OOP skills. I just learned it by myself. Too Bad.

Dennis wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 9:42 PM

Paano naman po yung OOP in SOA (Service Orient Architecture)?  I read OOP is not efficient within SOA as OOP is stateful and SOA is stateless.

lamia wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 10:25 PM

It totally depends on the scope and length of the project.  If I'm asked to do a very small program that there is .01% chance of it scaling then I don't mind programming in procedural code. In our webdev department here in our company I do not criticize the guys who still programs in procedural PHP because their deadlines are too strict to even have a plan. OOP is just a tool. One should not be a slave of its principles. You can achieve code reusability even if you don't program in a OO capable language. How do you think the video games back or even today which are coded in pure ASM were finished? They know how to organize code.

OOP is not a rule. It's an analytical tool.

cruizer wrote re: Procedural Object-Oriented Programming (POOP)
on 07-10-2007 10:33 PM

exactly. a service isn't typically object-oriented. it's more of procedural. so it's not realistic to expect an SOA interface to be "object-oriented."

Jon Limjap wrote re: Procedural Object-Oriented Programming (POOP)
on 07-11-2007 2:32 AM

Heh, I recently finished a project (sideline) that's pure procedural programming. I hated the parts where I had to copy-paste functionality, but I had to be quick about it so going OOP would be overkill.

Plus the fact that it's not possible to code genuine OOP in Visual Web Developer.

Rule of thumb, I think, is "what works best". Complex relationships among business rules entails OOP. That's it. Of course the word "best" in itself is subjective. Have to admit that.

jd2001 wrote re: Procedural Object-Oriented Programming (POOP)
on 07-13-2007 2:06 PM
undextrois wrote re: Procedural Object-Oriented Programming (POOP)
on 07-19-2007 4:35 AM

cool article. =)

phektus wrote re: Procedural Object-Oriented Programming (POOP)
on 07-28-2007 9:54 AM

true OOP starts from analysis to design to actual coding, while you can code procedural right there and then like a true hack

Agile_ph wrote re: Procedural Object-Oriented Programming (POOP)
on 07-30-2007 9:25 AM

Great read...question

How do we strike a balance with TDD and OOP concept of sending messages? I've only started doing TDD mid last year and much of the code i've produced is very procedural. Add refactoring, it gets cleaner but it's still procedural. Am I doing it wrong? How do you come up with OOP design/code if what you're trying to test, design and develop revolves around a procedural problem(probably, that's the challenge to it)...Am i'm I thinking out of context? :D

cruizer wrote re: Procedural Object-Oriented Programming (POOP)
on 07-30-2007 5:07 PM

hmm...nice observation there. i agree that initially my foray into TDD produced procedural-looking code. but later on I saw that using TDD + mock objects helped me create object-oriented code. the key is to make your methods short and test very narrowly-focused functionality only. mock objects also test method calls, not method results. this helps you lock in the "tell, don't ask" principle.

try doing it again and you might end up with very favorable results... :)

cruizer wrote Cyclomatic complexity and its effect on my code
on 09-20-2007 2:58 AM

Have you heard of the term cyclomatic complexity ? I had my first encounter with it in the current project

cruizer wrote Concurrency is all the rage in MSDN Magazine
on 10-02-2008 8:14 PM

The October 2008 issue of MSDN Magazine features lots of articles about parallelism and concurrency.

Copyright DevPinoy 2005-2008