Object Oriented Programming Isn’t Easy

Functional Procedural developers have it easy. Damn straight and I wonder why so many developers choose to write functions, when they can opt for more beautiful classes instead. I mean, I can’t understand why programmers opt to not torture themselves and other developers. It is easy to get functions right, all you have to do is prefix them and name them properly and you’re done.

When you start talking about encapsulation, inheritance, and polymorphism, people’s heads start to explode. People understand encapsulation, “Cool, you mean I don’t have to use globals and get to keep all of my variables in one place. WHAT? You also say that I can restrict in what people can access. Oh god, this is awesome!” People understand inheritance, “So basically, if I inherit from another object, I gain all of it protected and public properties and methods and can use them from within the child object. Ooooh. Parent -> Child relationship. Like, Mother and daughter, father and son.” Which explains why it is fairly easy to understand concept.

Where it starts to break down is “has-a” and “is-a.” To be completely honest, I have no idea what to make of the two. I’ve been failing enough times that I can get the feel of the two, but if you asked me to design something, I would just have to give you a blank stare. Doesn’t make me a bad programmer, I just think the code I write sucks. Not that it does suck, just that I should strive to never write code that looks that it again.

Is this code crap?

$result = Object::action()->find()->attribute1($value1)->attribute2($value2)->save();

There is a method named “where” that does absolutely nothing. I have a sense of humor.

Object::action()->update()
    ->attribute1($value1)->attribute2($value2)->save()
    ->attribute1($value3)->attribute2($value4)->save();

I have no idea what the pattern name is, but I know it allows chaining and that is perhaps a good thing, or something. It seems to be crap, but I can’t quite put my finger on why.

I hate Singletons too

The guy already didn’t have my attention, at first, by attacking my beloved, but the articles had good points, which I’ve expressed before. I think the issue is that programmers don’t know when to create a new object and refactor their existing object into two or more. Do they not think it would be worth the effort to create a new class or do they not understand the relationship the two would have? Or at least from what I’ve witness from awful and poor excuses for objects (it is as if the intention was to torture my mind).

The purpose of an object is to do one thing and do it well. I think everyone can grasp that concept. When you find that there are parts that don’t fit into that purpose, then naturally it should be assumed that a new object should be created. You may disagree, but whatever, anyone can understand this rule of thumb and at least it might save me from cutting myself to relieve myself from the pain of reading that person’s code.

I blame the Authors

They should be stoned, to death. I swear, the analogy of the wheel and the car means nothing to a programmer of novice status. What do cars and wheels have to do with code? The answer is nothing. Yet almost every crappy source (and I’m saying that any source that uses the car/wheel example is crap) uses it. Why? I think the way “has-a” and “is-a” was golden how I was able to become even more confused then before I started reading. The article that jump started the discussion explains the concept far better than anything I’ve read previously.

Why Can’t I?

I’m constantly on the quest to improve upon the terrible object-oriented code I write. It is really maddening how poorly it comes out. The Plan: Write awesome classes that does even awesome-er stuff. The Result: I must burn all traces of this code. Framework authors make class design patterns look easy. “Pfft. I can do that!” When I think how elegant HTML Purifier is, I weep because I can’t do the same.

The concepts of objects border on advance difficulty or at least the practice of doing them right. I’ve been wrestling with how to design a plugin model and threw away many ideas because they didn’t feel right (TDD is awesome for sensing crap by the way).

Possibly Related Posts:


16 Comments.

  1. I don’t think functional programming ( http://en.wikipedia.org/wiki/Functional_programming ) means what you think it means. I think the word you’re looking for is imperative programming.

    Anyway, when it comes to OO, use it when it makes sense. Use it when it makes your code cleaner and easier to follow. If simply organising your code into functions makes more sense to you, then do that.

  2. With your chaining of method-calls you probably meant Fluent Interface (http://martinfowler.com/bliki/FluentInterface.html).

    Furthermore, I think OOP is harder than it sounds mostly and to choose an appropriate solution for a problem is based mostly on experience. But when you learn more about OOP (by using literature in example) and you comprehend it, you do not have to make the experience yourself. It’s something about learning and experiencing (training).

  3. @skoob:

    You know, I was going to go with my gut and say “procedural,” but I heard others say that is wrong and that PHP is “Functional.” This means that not only can I kick their ass, but I can send them off to Wikipedia. I should probably also blow the dust off one of my programming books and find what they say about C being procedural.

    @Andre

    Thanks. I fully agree with you. OOP is a journey with no guide. At times you become lost and have to find your way, but it is always beautiful.

    I think it is perhaps funny, but only to me, that when I wrote plugins, I write them using functions and when I code projects, I use classes. Go figure, when I write plugins I need them to just work and not mess around with “l33t” coding style. There is a deadline and it has to work the first time. Coding projects is more free and open. Stuff has to work, but it also has to be manageable with options to reuse.

  4. Hey Jacob, OOP is definitely not the easiest concept to grasp in its entirety. I still find myself having to re-think long-held assumptions from time to time. If you can work your way past a few of the naive Car/Wheel type examples, I’d definitely recommend Object Oriented Thought Process. It was a light bulb a handful of years ago when I first started my exploration into OO understanding.

    About your code, it’s not crap, but I would name it a bit differently:

    Object::action()->getFinder()
        ->where(new ObjectCriteria('foo'))
        ->execute()

    Fluent, as Andre said, is the proper term for this style of API. The goal is to have code that reads like English. And one of the real reasons there’s a few "academics" or "purists" in the PHP community that would love to see base types become objects:

    $str->replace('foo', 'bar')->sub(-2)->toUpper()

    At any rate, if you’re ever looking for an excellent example of fluent APIs and their use, check out PHPUnit’s Mock Objects implementation. It is one of the best fluent interfaces I’ve seen thus far in PHP.

  5. I’ve learned Java, C++, PHP, and I once tried learning Haskell (which is sort of a _beginner_ functional programming language) and it absolutely kicked my ass.

    Functional programming is far, far more difficult to wrap one’s head around than object-oriented, IMHO. I encourage anyone who thinks they have a good bead on programming to go try it out. At the very least, it’ll give you some perspective.

    And afterward, you can explain monads in a manner that a normal person can understand, then you are the winner.

  6. Thanks Travis, I’ll look into that buying that book in the next couple of months and look forward to reading it. Hopefully it isn’t packed full of complicated concepts and just gets to the good stuff.

    Your code is what I was missing and is my point exactly. Why can’t I think, “Yeah, just use another object for searching.” It seems so obvious now that you bring it up, but I kept pondering about what was wrong or missing. I was content with the code. It worked and did its job, but I felt it could have been better.

    About drove me insane it did. If code is an art, then my style must be kindergarten finger painting. Thus is the quest for “beautiful” code. One day, I hope to be able to write sweet lines of code poetry that brings tears of joy to the eyes of those reading it.

    Which is why I think I’m enjoying C#, it is coding paradise. I just finished a project with bliss, who would have thought? I’m very much enjoying C#. At the very least, hopefully it teach me a thing or two about C++.

  7. If you want to learn OOP learn smalltalk. Just reading books about smalltalk will make you a better programmer even that you don’t use Smalltalk for real never.

    A list of books: http://www.iam.unibe.ch/~ducasse/FreeBooks.html

  8. You’re using PHP, of course OO sucks in it! Check out Smalltalk.

    Also, individual functions that can take any type are useful. For example: map and reduce. Tell me which class they would go into? A Utility class?

    "The concepts of objects border on advance difficulty or at least the practice of doing them right."

    That’s bullshit. Children can learn that easily when using Smalltalk/Squeak.

  9. @Rudolf Olah

    The concepts I talk about that are advanced are doing objects in a fashion where all the difficulty is abstracted away from the developer using the classes. There should not be so many layers between one layer of objects that I need to use to the solution I want to solve.

    Object design patterns serve to guide developers on doing this, but to learn about design patterns you must first know that they exist and it wasn’t until recently that I learned that there were actually ways to model objects to ease development.

    Objects themselves are not difficult and yeah, you are right in that aspect. However, in a discussion about a library, I offered suggestions on how the class could be separated logically out and was told that I was wrong. Okay, perhaps I was, but when you have comments telling about the separation of these methods from these methods. I don’t do that. When I come to that point, I split the classes instead of commenting how the class should have been split.

    Your use of “Utility” just motivates me to buy “PHP Design Patterns” and ” PHP 5 Objects, Patterns, and Practice” books that much faster. To think how much I’m missing out and how much I don’t know. In other words, “I have no idea what you mean when you say ‘Utility class’.”

  10. Hey Jacob – Design Patterns are a definite place you should grab a book for – I can’t guarantee it will solve the is-a has-a confusions ;) , but it does offer a really good look at why patterns are so talked about. They offer a perspective you could otherwise spend many years seeking on your own. Most will spend serious time talking about all those OO products like composition, encapsulation, orthogonality, coherence and coupling, etc and once you know a few Patterns these concepts make huge sense.

    Personally, OO drive me up the wall until I learned Patterns. Then just the coding drive me up to the ceiling until I learned TDD and Refactoring. I know from experience you have TDD ;) , so go grab the Patterns! :)

  11. "The goal is to have code that reads like English. " have a look at Ruby.

  12. Well, Johnny, I like to torture myself, so I think I’ll look into Python first. (Not that Python would be torture, just I like taking the hardest route so I can fail and gain some insight into why I failed.)

  13. Nice read! With OOP, it always seems that just when I get it, I read an article that describes some other tier of OOP programming that I have yet to wrap my brain around. I can’t wait for the day when I just understand it all, but for now I will keep plugging away at my own crappy code.

  14. Hope you don’t want to smash my face in for stating the obvious but perhaps a has-a/ is-a example from a programing perspective would help.

    If you take a company form as an object then a company abcence form is-a company form it can impliment the company form and save you time recreating the standard elements.

    A company form has-a text field for example. You can’t say a company form is a text field.

    Does this help or am I talking rubbish!