Tobias Schlitt describes Tim Bray’s talk at the International PHP Conference. (PDF slides) Tim compares PHP, Java, and Rails along several dimensions. One of those dimensions is maintainability. Tim ranks PHP as least maintainable, Rails in the middle, and Java as most maintainable.
This is not a surprising ranking. After all, Tim is from Sun, and the maintainability complaint is common in Anti-PHP rants. I’m not trying to suggest that Tim is anti-PHP, far from it, it seems. I’m just using his ranking as a spring board to ask questions.
Chances are that your average Java jockey or C scientist’s first exposure to PHP is to download one of the popular PHP applications. These are usually the product of some open source mega-project with developers of varying degrees of skill. Our engineer-by-day spends a few evenings with the program. The code is not technically outstanding.
How can something like this be so popular he asks? Yet, the software is successful by definition. Nobody downloads unsuccessful open source applications. The technocrat, heavily invested in his own technical prowess, faced with successful yet technically inferior code experiences cognitive dissonance. The only thing to do is to belittle the successful, but surely offensive code. “I could write better code than this,” he says, or “this code sucks,” or “this is unmaintainable.”
It is easy to dismiss these gripes inside the PHP community. After all, those of us using PHP professionally can write maintainable code in PHP. Ask any programmer and they will tell you, “My code is maintainable.” Who writes all of this unmaintainable code, anyway?
Lets take this gripe at face value for a moment. Why is PHP code considered hard to maintain? Is it the language that produces code that is hard to maintain, or is it that the popular ambassadors of the language happen to be programs that are hard to maintain?
Another common PHP sucks complaint is that PHP doesn’t scale. When you are talking about traffic, there are all sorts of counter examples for this. Personally, I’m dying to learn the story behind those .php extensions on YouTube. But, this post is not about requests per second.
Another kind of scalability is team size. I think that when some people complain that PHP doesn’t scale, what they mean is that PHP doesn’t scale to large development teams or large projects. Now we are back to the maintainability issue.
What is it about PHP that makes people think that it is not suitable for larger development teams?
The criticisms of maintainability and scalability generally come from outside the PHP community. But, there is a common complaint from within the PHP community.
It is hard to find a PHP wish list that doesn’t include namespaces. It comes up again and again.
Sometimes users request a feature without explicitly making their true desires and intentions known. They say “I want feature X,” but what they really mean is “solve problem Y.” Good programmers can hear the request for X, but make the jump to solving Y.
When people ask for the namespace feature, the problem they want to solve is integrating code from multiple parties. I wonder if the frequency of this request is a signal of a problem in this department? Perhaps one that requires more than just namespaces to solve? Is the namespace request a proxy for a larger problem?
What is it about PHP that makes it hard to integrate code written by multiple parties, whether they be different developers or different organizations?
I love programming with PHP, but for me (my) java code is easier to maintain. The reason is simple – java IDEs. They have a much better tools for code refactoring.
I have done very little Java programming so I can’t comment by comparison, but as a PHP developer one of the maintainability problems is the multitude of viable solutions to each problem. If I want to grab a file over http, do I use fopen, file_get_contents or cURL (there’s probably more options too). I like to use cURL but other developers make other choices and when you bring our code together the combined result is inconsistent. This inconsistency reduces maintainability.
As another example, if I want to use a MySQL database, do I use mysql, mysqli, or pdo_mysql?
I think there comes a point when old code and methods need to be retired. If others want to maintain compatibility with old functions via plug-ins and continue to maintain old, superseeded plug-ins they can. But the PHP developers need to take a more robust stand on not duplicating functionality by retiring poor functionality sooner when it has been superseeded.
However, even if this is done, there will still be a multitude of different techniques simply because PHP allows for procedural or OOP code (or a mixture of the two). I see similar problems when working with C++ – one programmers highly maintainable code is another’s confusion of stl containers, iterators, functors and generic programming. We’re all different. Maybe Java offers fewer choices?
The important thing is, if you have comprehensive coding standards indicating style of code and also what techniques to use (oop or procedural, mysqli or pdo, etc, a framework if appropriate) and implement them throughout the team then is your code easy to maintain for people who know those coding standards?
Having had to integrate existing apps inside other apps (usually something inside of Xaraya, *Nuke, or Zen Cart/OSCommerce) I join the desire for namespaces. But I think the need is because these existing apps are badly designed – there is already one variable $db in the global namespace and the second application you integrate also defines $db, but with a different DBA…
If everything was inside classes or functions and all were named Productname_XYZ then the problem would be less. It’s an old problem, and probably my least favourite of the lot because writing unstructured code is faster which for small open source projects is generally what the developer wants
Wow yes Youtube runs on PHP it seems.
You can add .php extension to any URL and it works, but not any other extension.
Jeff Moore’s Blog: Why is PHP Code Considered Hard to Maintain?
Although i agree with Richards post above about the complexities that arise from the multiple ways to do things.
I feel the major drawback is due to the ease of knocking something up everyone does, and in a commercial enviroment due to managment, programmers are forced to just add on functionality by slapping more functions etc in to add extra functionality to the original thrown together syatems rather than planning them in to work good.
Rather than a planned approach working in this sort of software development scheme just develops chaos i have coded in php for 4 years but my last job involved an app that had ‘evolved’ over a 5 year time span and in the 6 months i was there could not understand it for the life of me.
First I want to say that I love PHP and Java almost equally, but for different reasons. I would like to comment on namespaces, though, because I think that PHP programmers often don’t get why people want them so badly.
I don’t really want namespaces just for integrating other’s code (although it wouldn’t be a bad idea). Even for PEAR this would be nice. I often want to take a PEAR class (let’s make up one called FooFilter), wrap up the parameters I use most often and create my own class FooFilter. That’s a pretty standard pattern that I can’t do in PHP. I can have My_FooFilter. But that quickly makes for ugly code. Especially if I want to nest things. And it doesn’t scale well in development departments. Because ultimately you have “that guy”. And “that guy” doesn’t want to name his classes properly. Or he’s always too busy and he’s going to do it at the end of the project and it never happens. That’s one place Java is superior. It reigns in “that guy” a lot more and forces him to follow convention.
Another common place I use namespaces is in factories. For instance I might want do do something like this:
WidgetFactory.php
box/Widget.php
circle/Widget.php
polygon/Widget.php
Right now I have to do something like this:
WidgetFactory.php
widgets/Box_Widget.php
widgets/Circle_Widget.php
widgets/Polygon_Widget.php
which is fine as long as you don’t start needing lots of support classes for each widget. That’s where the namespaces really make the difference between a directory full of files and a nice hierarchy.
And the lack of lexical scoping. That’s bad. Again it makes for hard to maintain code. It’s not an issue if you just have a few people who are really good, and can be trained to follow instructions, but again if you have to hire “that guy” for some reason (and every dev department over about 10 people is probably going to end up with “that guy”), you’re going to end up with all sorts of horrid impossible to troubleshoot problems.
I don’t think either of these are deal breakers, but I think it does balance out with the productivity losses you lose to Java by having to constantly wait to redeploy code to your application server.
One problem is that many people writing PHP code, regardless of PHP 4 style or PHP 5 OO style, still do things like putting HTML markup inside echo statements, into variables that then get echoed, or worst of all, they put HTML fragments into the database alongside business data.
A consistent separation between data, code, and presentation should make it easier to maintain all three independently of one another.
Not that this is a failing particular to PHP — you can do this in most languages.
I believe that namespaces would go a long way to improving current systems of package integration, but I do also think that there’s a bigger picture to the namespaces request. The problem is that PHP apps want to interoperate, and the reason why it’s often tricky is probably at its simplest “lack of standards”. Of course Java has no shortage of — some built into the language (e.g. foundation classes), some in specs, etc. Rails too, is completely convention-driven in a way that doesn’t make sense to compare it to PHP (I guess that’s the heart of the framework-vs-lang comparison problem). Currently PHP projects can use any number of solutions to handle logging, database access, configuration management, etc., etc. There’s rarely an agreed upon tool for the job or built-in language tools for most of these basic application needs.
I do think the situation is getting better & better in PHP, though. At the engine level PHP5 has introduced a number of built-in classes (e.g. Exceptions, SPL, DateTime) which provide OO standards for error handling, date-time manipulation, iteration, object loading, etc.
Now there are E_STRICT-compliant foundation libraries like eZ Components (and Zend_Framework) providing a good set of tools that all our apps need. I hope that PEAR too will start requiring E_STRICT packages, fix their namespace issues, etc. It does look hopeful.
Anf finally, I think one of the greatest recent improvements in the PHP world is the new PEAR installation system which allows anyone to distribute components *with* dependencies on other components. This is going a long way in making it possible for components to work together in PHP.
I do think namespaces are a critical component to this big picture of inter-operating components in PHP. The PHP community has really grown up over the past few years, driven by these new language features — and now in turn is pushing for new features (like namespaces).
Hans
http://youtube.com/t/jobs#sweng
I don’t see any of these lines mentioning “PHP”, instead I saw Python. How so?
* Scope is a real issue – novice developers will just dump everything into a global scope and with lack of namespaces (or strict discipline), collisions ensue.
* Legacy code – PHP4 was pretty ugly, horrible object support, encouraged widespread use of globals (you had to do some ugly hacks to even get a Singleton working). Most ’successful’ applications were developed under PHP4.
* Legacy Stupid Configuration Defaults – register_globals, magic_quotes, etc. Lots of people developed for these defaults without realizing the mess they were creating.
That said, PHP5 code used with thought and some good libraries can be quite maintainable.
[...] November 10th, 2006 in Links A good analysis of why people consider PHP difficult to maintain?. My observation is that it isn’t, unless the code is structured poorly (an affliction possible in most languages), or the developer is reliant on features in higher-level IDEs. [...]
Re: Tim’s comment.
Isn’t it that you are trying to work in PHP as in java?
.
now, i actually do not know PHP
(why is this guy commenting then here?!?!?!?
), but i know java and python, and i see it many times that java programmers start to use python, but they don’t really want to use it “the python way”, and basically are writing java code with python syntax.
as i said, i do not know PHP, so it’s quite possible that i’m wrong, but the whole “factory” thing is really, really java like.
Ed, since Google brought them out and Google is a python shop it is natural for them to start moving over to using python on youtube, considering the the BDFL of Python is another Google employee.
[...] Muchas voces han salido al paso de las declaraciones de Tim. Aquà os dejo una interesante opinión. [...]
[...] While scanning the list I came across a post yesterday on Professional PHP, which is 3 weeks old but very pertinent given the languages I’m developing with. The post, entitled “Why is PHP considered hard to maintain”, covers Tim Bray’s presentation given at the International PHP Conference in which Tim compares the relative merits of Java, PHP and Ruby. We aren’t just talking about the languages themselves, but also the framework (for example, JSF/Spring/Hibernate, Apache/mod_php, Ruby on Rails) that allows you to build web applications; of course, you have to be careful which criteria you choose to compare but I think Tim’s presentation succeeds. I’ll cover the point about PHP maintainability below. [...]
Personally for me: If I change a function call and add a new parameter for an example, I can’t just recompile the whole project and easily see all function calls that need to be fixed. I have to take extra steps in php or you can call it hard maintainability.
Depends on the scale of your project and your developers, PHP codebase can be maintained very effective. But when comparing directly with Java, I have to agree that Java has a more structured and consistent principle (from naming convention to all kinds of syntax).
It is a problem of PHP that it started as a non OOP scripting language, languages as python or java got ahead of it. It is fashion of java coders who just wrote program hallo word to get at PHP.
I did not read a study where PHP would not be twitted for any of its mistakes.
Compare perhaps Lisp with PHP in maintainability
Personally, I think most problems stem from trying to force a complex solution on PHP. PHP likes simple solutions. The more you try to do in one request the less maintainable and scalable the code.
I think PHP follows in much the same footsteps as HTML, but has somehow got a bad rap. Once upon a time (okay, fine, its not over) there was HTML tag soup but then people decided to do a better job with practically the same tools and options available to them, and you now have a whole webstandards movement. In the same way, the coding conventions (from where you put new lines all the way to how you construct your apps, like using OO and you bloody well better not intermingle your HTML and PHP) have matured. While we all strive for ruby-style elegance in our PHP5 projects, let’s not forget our history and the importance of the frontier days. HTML3/4 and PHP3/4 were VERY useful; the ability for anyone to bash something together is a cornerstone of the mass-developer innovation rampant on the internet.
I will second the mixed language problem. I’ve seen and written some horrible PHP code. In my case its usually caused by mixing PHP/HTML and javascript all into the same mess. The problem is that its so easy, and there are so many example on how to do it wrong, that it appears everyone starts off on the wrong foot. Once you gain some experience you soon discover how bad the code really is.
Its like C/C++, lots of people talk about how great the K&R book is. When you go back and read it after a few years experience, you discover that the examples are good for instruction but absolutely terrible code. 90% of the examples in that book utilizing pointers could be written without the pointers and be just as fast, 10x as readable, and nearly all the resulting bugs/gochas would just disappear. Yet, that doesn’t loads of projects being full of the same tired old crap.
PHP needs to be redesigned. Its inconsistencies a PITA.
var $variable; // is valid/error depending on where you it
static var $variable; // is an error, correct usage: static $variable
self::$variable but $this->variable // why not make it self::variable???!!!!
The method/function name inconsistencies are too numerous to be listed here.
PHP NEEDS TO BE REDESIGNED!
Hey Dan,
What seems to be the problem with those examples??
There’s no problem with php as a language. It’s the user that write the “unmaintainable” code.
And as for scaling, Digg.com one of the net’s biggest high traffic sites uses php as their programming language.
PHP is no different to any other language, there are situations where it’s perfect and other situations where it’s not; saying that one language is superior over another is therefore a bit of a none starter. PHP’s beauty is its flexibility, that can also be a shortcoming in situations that it is ill suited.
Your site found in Google: http://google.com/search?q=hmp
[...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]
[...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]
[...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]
[...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]
I have not run into any unmaintainable code in any computer language until people start putting in idiotic “frameworks” like MVC in languages that aren’t inherent to it or FuseBox in ColdFusion.
Be it BASIC, ColdFusion, PHP, or Perl.
So… you argue that PHP sucks because it doesn’t have namespaces. Well, now it does! Congratulations, you suck!
Oh, wait… The chosen namespace separator is BACKSLASH?!? And this was decided by a couple of incompetent programmers over an IRC conversation? I guess I should tell you I’m sorry for insulting you and I have to agree with you: PHP sucks!
I’ve been using PHP for quite a while now and I’ve been waiting to see a lot of improvements. The language has seen some improvements, but not the ones we’ve expected…
I feel the major drawback is due to the ease of knocking something up everyone does, and in a commercial enviroment due to managment
great work !!
I guess I should tell you I’m sorry for insulting you