I was doing my daily browsing of the sitepoint forums and was visiting a topic we have started regarding Hibernate and OQL. One of the posts in that topic said pretty much the following:
[quote]Oh i forgot to mention that Exceptions are a prime example of something that doesnt fit the PHP way at all![/quote]
Now I don’t exactly know what the "PHP" way is. But a statement like this makes ABSOLUTELY no sense to me. I decided against posting my thoughts at sitepoint as this would undoubtedly ruin an otherwise informative thread.
Now thankfully I was able to get a better idea of where this guy was coming from since he posted a comment at the following blog post:
Unplanned OOD = Planned Disaster
Quite frankly, after reading his post what he said in it makes no sense to me. Since when does PHP have to be a rapid application deployment tool? I will admit that it fit that bill pretty well back in php4. However, I thought the intent for php5 was to cater to object oriented designers and quite frankly if you intend to just launch yourself straight into programming completely undefined or unplanned interfaces you are going to have a somewhat ineffective and unscalable program resulting. I will agree that there are some types of people that put waaaay too much emphasis on modeling. However to completely ignore modeling means you will be designing as you go and that will often lead to lots of code rewrites and headaches down the road.
Lack of a model makes unit testing harder. How can you write test cases without knowing what you will be testing? He mentiones that the real problem in programming is to get the interface to the user just right. Now I don’t exactly know what this means. In either case how can you tell exactly what the user needs without first determining a model that will fit the user’s needs? Surely you wouldn’t write all the code and then say "Does this work for you?" What happens if they say no? You basically have to go through a large chunk of the development process all over again.
One good point he did make was the lack of OOM tools for PHP5. Compared to Java PHP5 has pretty much nothing in the realm of OOM tools. I don’t see this remaining the case for very long however. I estimate by the end of the year php5 will have become mainstream enough to warrant the attention of some of the UML tool creators. I would wager to guess someone, somewhere has already started a PHP5 add-on for Poseidon. There are also plenty of tools out there that can speed ub development such as Phing, Propel, SimpleTest. So just give it some time and OOD in PHP5 will be as close to painless as possible.
Also, it is not our job to explain UML diagrams to customers and it’s not their job to understand them. Interactions with the client should be done through phased releases. To expect them to understand all of the "programmer’s lingo" is arrogant. If there isn’t someone on your design team who can communicate with a client in terms they can understand then it’s time to hire some new team members. I understand wanting to get a full prototype out to a customer, but this just seems like a recipe for disaster. Why not just take the time to plan one phase of the project then while you are developing that phase have someone else (or even do it yourself when you get bored with coding) start another phase. The requirements and interface for the first phase components will already be set so there is no reason not to start planning while coding.
Ok it seems I have gone off on a tangent. So wy are exceptions bad? Because they force you to write finished code. Finished bug free code. But that means you cant quickly make some modifications to your application to help your users become something that feels a bit more like what they have in mind. They dont need bug free code for that. They need to be able to see and touch the prototype in regards to the specific thing you are currently trying to visualize for them. Even if that means that you have an error string in a dropdown instead of a list of all countries that should normally go there.[/quote]
Why not write finished bug free code? I would rather be able to hand a client a piece of working, bugfree, finished code before I ever gave them a prototype that really doesn’t do a whole heck of alot. If you can’t code around exceptions and come up with working code quickly than you have some serious problems that need to be addressed.
Why Do I Like Exceptions?
Well, the biggest reason is it helps you seperate your code. I remember many days spent in php4 trying to tie an error handling class into my application. Then I would run accross someone elses code that I wanted to integrate and it handled error handling completely different. There was no elegant way to recover from non-fatal errors.
Exceptions in php5 now solve lots of problems. It gives developers access to a widely used and widely understood and highly compatible interface by which to handle error management. It is extremely simple to recover from non-fatal errors. It is also extremely effective at validating input. It makes it easy to keep error generation within the domain/data acces/etc. layers while keeping the error handling within the application layer where it belongs.
Well, that pretty much wraps up a broad overview of my thoughts on this matter. While I’ll agree that you can use exceptions badly, I fully disagree that there isn’t a place in php for them.