In a new post to his blog, Jacob Santos talks about why object oriented programming just “isn’t easy” - including a few of his dislikes about OOP in PHP.
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?
PHP:
-
$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.
PHP:
- 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).
Tags: Code, Design, Design Patterns, HTML, Object Oriented, Perl, PHP, Programming, Variables














No user commented in " Object Oriented Programming Isn’t Easy "
Follow-up comment rss or Leave a Trackback