web programming [with php rant]
comments | Posted in php | rails | rant | ruby on Wednesday, September 12 2007 22:01:00 PDT
A long time good friend of mine asked me this over email this morning:
hey, do you use Ruby? I remember you was talking about PHP sucks, which I am agree. can you tell me little bit about whether Ruby is better than PHP? Or what is the best language for web development in your mind?
And he requested I make it a blog post. I dont mean to rant or rave in this posting but I will lay out the facts as I know them in my 3-4 years of web development.
I am familiar with: JSP, ASP, RoR, Camping, PHP, CakePHP, CodeIgniter and Coldfusion 7. I have either developed from scratch site(s) in the above languages, or maintained an existing codebase created in the above languages. I am not necessarily an expert in either language, but am proficient in many of the languages/frameworks.
Is Ruby better than PHP?
As a programming language, yes, Ruby is much more robust and complete language that is more cohesive than PHP4 or PHP5. Ruby performs the job of scripting tasks better than PHP. My opinion is that Ruby's clearer syntax allows me to complete tasks quicker, and when I have to maintain a codebase 4 months after creation ... Rubys english-like syntax allows me to more easily digest what I previously wrote.
Examples where PHP seems less polished than other languages:
- Having only function or global scope with no form of namespaces in PHP really sucks. Since nothing should be global (I have used Fortran77 and GLOBAL scope before), and we have no namespaces every single controller has to do something like
<?php
source: http://manual.cakephp.org/chapter/controllers
$this->set('users', $this->requestAction('/users/getUserList')); ?> the transparency of MVC is lost here. The variables I have created in my controller for the given action should be available to the view and having to explicity set them this way adds an un-necessary level of indirection.
PHP frameworks may copy RoR (many do) but due to PHPs poor design will never really compete at innovating web frameworks. RoR did it 1st and best. (ok, seaside did it first ... but RoR won the popularity contest :P) [see point 1]
PHP5 may have added features to the language which make it more capable with Object Orientation. But overall, the language still feels tacked-together and will never fully have a similar power equal to Ruby in terms of expressiveness, capability, or elegance. Examples:
- C style functions suck.
<?php $ret = preg_match('/showmethemoney/i', $value, $matches); ?>If I wanted to code with the hardware and memory abstractions of C I would use C. For the love of god let me type:matches = value.match /showmethemoney/iLet me worry about the problem at hand and how to solve it and not about mundane details regarding the memory implementation of the underlying hardware. poor heredoc support. Many languages support the heredoc syntax for a string. Ruby allows for:
def foo val =<<-EOFMSG I am a string. EOFMSG endPHP does not allow for your end of heredoc marker to be anywhere but column 1 of the file. For example:<?php function foo() { $val =<<This breaks the visual lexical scope of the page when I print out sourcecode. The block structure of source files, and therefore proper indenting mean something for readability of code. PHP goes out of its way to break the visible structure of my code.} Stupid reference operator. Ruby allows for . and :: as a resolution, dereference or scope operator. PHP allows for :: or -> to denote scope. Why force me to type -> when . will do just fine. I don't like typing extra characters and its annoying to have to read things like found here:
<?php $refA = new ObjectA; echo($refA->getObjectB()->getValueA()); //this is chaining / or another example / $picasaimageUrl = $picasawebxpath->query('.//media:content/@url', $picasa_node)->item(0)->textContent; //deep chaining ?>I see this method chaining as a poor hack for not language not supporting closures. Method chaining will only abstract your code insofar as you can clearly define a method of every usage of the object you may have. Without closures doing critical in-between business logic you may as well make the code more readable and break everything up into normal linear function calls and save off the resulting values.<?php $foo= Baz::value(); ?>Closures let you NOT have to have a million little methods so that you could chain them all together as needed. They let you place the glue code in-place ... but in a readable, English fashion [There are no $ -> () symbols littering the code everywhere]. Ruby syntax is for the most part cleaner.The governing body of PHP has allowed for a mass of inconsistently named function names in the core php library, come on we are at version 6 (nearly).
Complex string evaluation does not allow for easy function embedding withing a string (Ruby allows this).
<?php
This wont work in heredoc format either.
echo "This wont work {my_magic_number()}"; ?> I like heredoc syntax because it can make emitted xml/html code much cleaner to read, but sometimes I wish I could embed function calls into the string as well without first terminating the string and then concating.
- PHP allows for some akward syntax, which I think breaks readability. My coding standard is to put operators at the end of a expression if the expression would continue on more on than one line, as reading in English we go top to bottom and left to right. Seeing dangling commas is bad practice and used to be frowned upon back when I did C.
<?php
done in Drupal core all the time.
$foo = array(
'one' => 'hihi',
'two' => 'heyhey',
'three' => 'neato',
); ?>
What is the best language for web development in your mind? (woof, what a loaded question :P)
I think that dynamic languages are currently best suited for the web. I want to code something from scratch with the least amount of overhead. I don't want to generate a blob of skeleton code to get the task done (aka java) and I want immediate feedback on whether I broke the page or completed the task. PHP, ASP, RoR allow for this instant feedback.
So what is the best framework/language for a site?
This depends on the task at hand. I have used Camping to write a handful of internal business tools to speedup our development processes. Camping allows for a consistant application structure with very little pre-thought on design/implementation. It just gives me a clean way to to try and hack out a solution to a problem.
In terms of readability and maintainability I think that nearly any MVC framework will buy you maintainability. But readability is added by a clean syntax ... which in my opinion Ruby has in spades. So I would opt for a Ruby based solution to designing and implementing a website (I actually really like Markarby even :P ... compiled checking of html!!! wooohooo!).
Obviously when performance becomes an issue, "clever algorithms" or a more complex program/database structure may speed up your site. But, I do not want to address performance in this post. I believe that a robust, performant solution to any problem will 99% of the time be more complex (and therefore harder to look back on in the future) than the initial best guess solution/implementation initially used in the deployment of the system.
So, that in brief on some thoughts on PHP and web the current state of web programming.
happy coding :)
EDIT: silly slapstick remark removed from post.